Ct Assignment
Ct Assignment
Name : P . tejasmani
Question -1
https://www.hackerrank.com/challenges/lilys-homework/problem
Whenever George asks Lily to hang out, she's busy doing homework. George wants to help her finish
it faster, but he's in over his head! Can you help George understand Lily's homework so she can hang
out with him?
Consider an array of distinct integers, . George can swap any two elements of the array any number
of times. An array is beautiful if the sum of among is minimal.
Given the array , determine and return the minimum number of swaps that should be performed in
order to make the array beautiful.
Function Description
Returns
swaps = 0
for i in range(len(arr)):
if visited[i] or position[arr[i]] == i:
continue
cycle_size = 0
j=i
visited[j] = True
j = position[arr[j]]
cycle_size += 1
if cycle_size > 1:
swaps += (cycle_size - 1)
return swaps
sorted_arr = sorted(arr)
descending_arr = sorted_arr[::-1]
arr = [2, 5, 3, 1]
print(lilysHomework(arr))
question-2
Sorting
One common task for computers is to sort data. For example, people might want to see all their files
on a computer sorted by size. Since sorting is a simple problem with many different possible
solutions, it is often used to introduce the study of algorithms.
Insertion Sort
These challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start
with a nearly sorted list.
Since this is a learning exercise, it won't be the most efficient way of performing the insertion. It will
instead demonstrate the brute-force method in detail.
Assume you are given the array indexed . Store the value of . Now test lower index values
successively from to until you reach a value that is lower than , at in this case. Each time your test
fails, copy the value at the lower index to the current index and print your array. When the next
lower indexed value is smaller than , insert the stored value at the current index and print the entire
array.
Function Description
Returns
None: Print the interim and final arrays, each on a new line. No return value is expected.
def insertion_sort_steps(arr):
key = arr[-1]
i = len(arr) - 2
i -= 1
arr[i + 1] = key
# Input
# Function call
insertion_sort_steps(arr)
question-3
Sorting
One common task for computers is to sort data. For example, people might want to see all their files
on a computer sorted by size. Since sorting is a simple problem with many different possible
solutions, it is often used to introduce the study of algorithms.
Insertion Sort
These challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start
with a nearly sorted list.
Since this is a learning exercise, it won't be the most efficient way of performing the insertion. It will
instead demonstrate the brute-force method in detail.
Assume you are given the array indexed . Store the value of . Now test lower index values
successively from to until you reach a value that is lower than , at in this case. Each time your test
fails, copy the value at the lower index to the current index and print your array. When the next
lower indexed value is smaller than , insert the stored value at the current index and print the entire
array.
Example
Start at the rightmost index. Store the value of . Compare this to each element to the left until a
smaller value is reached. Here are the results as described:
12455
12445
12345
Function Description
Returns
None: Print the interim and final arrays, each on a new line. No return value is expected.
Input Format
The first line contains the integer , the size of the array .
The next line contains space-separated integers .
def insertion_sort_part1(arr):
n = len(arr)
i -= 1
# Input
n = int(input())
insertion_sort_part1(arr)