Monday, 2 September 2019

The Joy of Computing using Python: Programming assignment 1,2,3

Programming Assignment-1: Cab and walk

Arun is working in an office which is N blocks away from his house. He wants to minimize the time it takes him to go from his house to the office.
He can either take the office cab or he can walk to the office.
Arun's velocity is V1 m/s when he is walking. The cab moves with velocity V2 m/s but whenever he calls for the cab, it always starts from the office, covers N blocks, collects Arun and goes back to the office.
The cab crosses a total distance of N meters when going from office to Arun's house and vice versa, whereas Arun covers a distance of (2N) while walking.
Help Arun to find whether he should walk or take a cab to minimize the time.

Input Format:
A single line containing three integer numbers N, V1, and V2 separated by a space.

Output Format:
Print 'Walk' or 'Cab' accordingly

Constraints:

1<=V1, V2 <=100

1<=N<=200

Example-1:

Input:
5 10 15

Output:
Cab

Example-2:

Input:
2 10 14

Output:
Walk

Code:

n,v1,v2=input().split(" ")
n,v1,v2=int(n),int(v1),int(v2)
tv1=(2**.5*n/v1)
tv2=(2*n/v2)
if(tv1>tv2):
  print("Cab",end="")
else:
  print("Walk",end="")

Programming Assignment-2: End-Sort

Given a list A of N distinct integer numbers, you can sort the list by moving an element to the end of the list. Find the minimum number of moves required to sort the list using this method in ascending order. 

Input Format:
The first line of the input contains N distinct integers of list A separated by a space.

Output Format
Print the minimum number of moves required to sort the elements.

Example:

Input:
1 3 2 4 5

Output:
3

Explanation:
In the first move, we move 3 to the end of the list. In the second move, we move 4 to the end of the list, and finally, in the third movement, we move 5 to the end.

Code:

x=[int(x) for x in input().split()]
x1=sorted(x)
c=0
for i in range(len(x)):
    if x[i]==x1[c]:
        c=c+1
print(len(x)-c,end="")

Programming Assignment-3: Semi Primes

A semiprime number is an integer which can be expressed as a product of two distinct primes. For example 15 = 3*5 is a semiprime number but 9 = 3*3 is not .
Given an integer number N, find whether it can be expressed as a sum of two semi-primes or not (not necessarily distinct).

Input Format:
The first line contains an integer N.

Output Format:
Print 'Yes' if it is possible to represent N as a sum of two semiprimes 'No' otherwise.

Example:
Input:
30

Output:
Yes

Explanation:
N = 30 can be expressed as 15+15 where 15 is a semi-prime number (5*3 = 15)

NOTE: N is less than equal to 200

Code:

prime1=[]
prime2=[]
semiprime1=[]
semiprime2=[]
last=[]
n=int(input())
for i in range(2,n):
    for j in range(2,i):
        if(i%j==0):
            break
    else:
            prime1.append(i)
            prime2.append(i)

for i in prime1:
    for j in prime2:
        if i!=j and i*j<=n:
            semiprime1.append(i*j)
            semiprime2.append(i*j)
semiprime1.sort()
semiprime2.sort()

for i in semiprime1:
    for j in semiprime2:
        if(i+j<=n):
            if (i+j) not in last:
                 last.append(i+j)
last.sort()
if n in last:
    print("Yes",end="")
else:
    print("No",end="")

15 comments:

  1. Nice Blog!!! keep Posting!!!

    Trekking And Camping in Dharamshala Dhauladhars Range – Kangra Valley –
    Drop us a line and we will plan these treks for you! We will make it come true. No matter how strange or wild or tricky.
    Himalaya Cab Provide Best Taxi services in dharamshala & kangra.
    We offer pickup & dropoff Taxi services from Dharamshala airport to Mcleodganj.
    Dharamshala taxi services www.himalayacab.com


    Best Taxi Service in Kangra,
    Best Tax Service in Mcleodganj, Best Taxi Service in Gaggal Airport, Best Taxi Service in kangra Airport, Best Taxi Service in Dharamshala Airport

    ReplyDelete
  2. Nice Article and keep writing 👍A preferred taxi/cab service in Dharamhsla.
    Dharamhsla taxi service

    ReplyDelete
  3. I was under a tight deadline, but Python Assignment Help delivered my assignment on time. Their punctuality is commendable.
    Python Assignment Help

    ReplyDelete