0% found this document useful (0 votes)
1 views

Ct Assignment

Uploaded by

P.tejasmani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Ct Assignment

Uploaded by

P.tejasmani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Comptutional thinking assignment

Name : P . tejasmani

Roll number: nc.ai.u4aid24055

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

Complete the lilysHomework function in the editor below.

lilysHomework has the following parameter(s):

int arr[n]: an integer array

Returns

int: the minimum number of swaps required


def lilysHomework(arr):

def count_swaps(arr, target):

position = {value: idx for idx, value in enumerate(target)}

visited = [False] * len(arr)

swaps = 0

for i in range(len(arr)):

if visited[i] or position[arr[i]] == i:

continue

cycle_size = 0

j=i

while not visited[j]:

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]

ascending_swaps = count_swaps(arr, sorted_arr)

descending_swaps = count_swaps(arr, descending_arr)


return min(ascending_swaps, descending_swaps)

arr = [2, 5, 3, 1]

print(lilysHomework(arr))

question-2

Insertion Sort - Part 1 | HackerRank

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.

Insert element into sorted list


Given a sorted list with an unsorted number in the rightmost cell, can you write some simple code to
insert into the array so that it remains sorted?

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

Complete the insertionSort1 function in the editor below.

insertionSort1 has the following parameter(s):

 n: an integer, the size of

 arr: an array of integers to sort

Returns

 None: Print the interim and final arrays, each on a new line. No return value is expected.
def insertion_sort_steps(arr):

# Get the last element to insert into the sorted portion

key = arr[-1]

i = len(arr) - 2

# Traverse the array from right to left

while i >= 0 and arr[i] > key:

arr[i + 1] = arr[i] # Shift element to the right

print(*arr) # Print the current state

i -= 1

# Place the key in its correct position

arr[i + 1] = key

print(*arr) # Final state

# Input

n = int(input()) # Number of elements

arr = list(map(int, input().split())) # Array elements

# Function call
insertion_sort_steps(arr)

question-3

Insertion Sort - Part 1 | HackerRank

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.

Insert element into sorted list


Given a sorted list with an unsorted number in the rightmost cell, can you write some simple code to
insert into the array so that it remains sorted?

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

Complete the insertionSort1 function in the editor below.

insertionSort1 has the following parameter(s):

 n: an integer, the size of

 arr: an array of integers to sort

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)

key = arr[-1] # Last element is the key

i = n - 2 # Start comparing from the second-to-last element

while i >= 0 and arr[i] > key:

arr[i + 1] = arr[i] # Shift element to the right

print(*arr) # Print the current state

i -= 1

arr[i + 1] = key # Insert the key in its correct position

print(*arr) # Print the final state

# Input

n = int(input())

arr = list(map(int, input().split()))


# Function call

insertion_sort_part1(arr)

You might also like