You are viewing a single comment's thread. Return to all comments →
a=[int(x) for x in input().strip().split(' ')]
for i in range(1,len(a)):
while j>=0 and a[j]>key:
Do let me know if there is more finer solution in python.
loved the usage of *a. Learnt something new instead of using join()
You can't use join, the list doesn't contain strings but integers.
you can use join...
print(' '.join(str(i) for i in array))
it can be used because python provides str() functions which can convert interger to strings
I am glad you learned something new..Do let me know if i can help you with something.
Awesome! and thanks for the help offered. What would be in your opinion are good resources to learn advanced python concepts and also get better at Data Structures and Algorithms?
In case of Algorithms and Data structures I would reccomend you to learn from "CLRS" and even i do the same its a very good book.
In case of Advanced Python i suggest you to join online courses because it's a language the more you do the more you learn.
Cool. Thanks! I seem to be on the right path then
I did about the same, but using a sentinel at the front of the array:
n, amin = int(input()), -10**4
A = [2*amin] + list(map(int, input().split()))
for j in range(2, n+1):
i, a = j, A[j]
while a < A[i-1]:
A[i] = A[i-1]
i -= 1
A[i] = a
Thanks,learned something new.
Awesome, new sentinel concept learned. Thanks for sharing this!
for i in range(1, len(ar)):
for j in range(i):
k = ar[i]
ar[i] = ar[j]
ar[j] = k
I think its bubble sort!
In the modern HackerRank interface, my Python3 solution uses the in-built insert for lists.
def insertionSort2(n, arr):
for i in range(1, n):
j = 0 ; key = arr[i] ; del arr[i]
while j < i and arr[j] < key : j += 1
arr.insert(j, key) ; print(*arr)