Build Your Algo | Practice Coding | Programming Language |
BUILD YOUR ALGORITHM
CODE !
( Any programming language , Build in any logic , Practice Coding)
Q. Take a list of items, arrange them all odd first and then even, last sort them in acceding order the odd and then even.
** Time : 30 mins **
Sample input :
[1,2,3,4,5,6,7,8,9,10]
Sample output :
[1,3,5,7,9,2,4,6,8,10]
Tips:
** Build your algorithm**
HINTS :
1. Check list is empty
2. Arrange in odd then even
3. Find the mid value
4. Sort and merge the items
6. And return list.
PYTHON CODE :
mylist = [1,2,3,4,5,6,7,8,9,10] def mylist(alist): # check list is empty if len(alist) == 0: return 0 # arrange in odd then even for i in range(0,len(alist)): if alist[i]%2 == 0: for j in range(i,len(alist)): if alist[j]%2 == 1: X = alist[i] alist[i] = alist[j] alist[j] = X # find the mid value mid =0 for idx in range(0,len(alist)): if alist[idx]%2 == 0: mid = idx break if mid == 0: alist.sort() return alist # sort and merge the items return(sorted(alist[0:mid]) + sorted(alist[mid:len(alist)])) print(mylist(mylist))
OUTPUT :
---------------------------------------------------
Related Links :
Download: Python
Install : python
---------------------------------------------------
Comment below for any help !!
-----------------------********--------------------
This comment has been removed by the author.
ReplyDelete