0% found this document useful (0 votes)
3 views5 pages

Python Exercises

The document contains a series of Python exercises that cover various programming concepts such as robot movement, dictionary and list manipulations, Fibonacci sequence, binary search, factorial computation, and counting even/odd numbers. Each question is followed by a sample solution in Python code. The exercises are designed to enhance programming skills and understanding of basic algorithms.

Uploaded by

wrheisenberg1979
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python Exercises

The document contains a series of Python exercises that cover various programming concepts such as robot movement, dictionary and list manipulations, Fibonacci sequence, binary search, factorial computation, and counting even/odd numbers. Each question is followed by a sample solution in Python code. The exercises are designed to enhance programming skills and understanding of basic algorithms.

Uploaded by

wrheisenberg1979
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON EXERCISES

Question 1.
A robot moves in a plane starting from the original point (0,0). The robot can move
toward UP, DOWN, LEFT and RIGHT with a given steps. When user enters EXIT,
the program is finished and prints the final distance from the point (0, 0). The trace of
robot movement is shown as the following: UP 5 DOWN 3 LEFT 3 RIGHT 2. The
numbers after the direction are steps.
Please write a program to compute the distance from the current position after a
sequence of movement and original point. If the distance is a float, then just print the
nearest integer.
Example: If the following tuples are given as input to the program: UP 5 DOWN 3
LEFT 3 RIGHT 2 Then, the output of the program should be: 2
Hints: In case of input data being supplied to the question, it should be assumed to
be a console input.

Solution:
import math
pos = [0,0]
while True:
s = input("Enter position and step:")
s= [Link]()
if not s:
break
if s == "EXIT":
break
movement = [Link](" ")
direction = movement[0]
steps = int(movement[1])
if direction=="UP":
pos[0]+=steps
elif direction=="DOWN":
pos[0]-=steps
elif direction=="LEFT":
pos[1]-=steps
elif direction=="RIGHT":
pos[1]+=steps
else:
pass

print(int(round([Link](pos[1]**2+pos[0]**2))))
Question 2.
Define a function which can print a dictionary where the keys are numbers between
1 and 20 (both included) and the values are square of the keys.

def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
return d
result = printDict()
print(result)

Question 3.
Define a function which can generate a list where the values are square of numbers
between 1 and 20 (both included). Then the function needs to print the first 5
elements in the list.
def printList():
li=list()
for i in range(1,21):
[Link](i**2)
print(li[:5])

printList()

Question 4.
Define a function which can generate and print a tuple where the value are square of
numbers between 1 and 20 (both included).
def printTuple():
li = list()
for i in range(1, 21):
[Link](i ** 2)
print(tuple(li))

printTuple()

Question 5.
Write a program to compute:
f(n)=f(n-1)+100 when n>0 and f(0)=1
with a given n input by console (n>0).
Example: If the following n is given as input to the program: 5
Then, the output of the program should be: 500
def f(n):
if n==0:
return 0
else:
return f(n-1)+100

n=int(input())
print(f(n))

Question 6. The Fibonacci Sequence is computed based on the following formula:


f(n)=0 if n=0 f(n)=1 if n=1 f(n)=f(n-1)+f(n-2) if n>1
Please write a program to compute the value of f(n) with a given n input by console.
Example: If the following n is given as input to the program: 7
Then, the output of the program should be: 13
def f(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return f(n-1)+f(n-2)

n=int(input("enter number"))
print(f(n))

Question 7. Please write a binary search function which searches an item in a sorted
list. The function should return the index of the element to be searched in the list.
If the element does not exist, return -1.
import math
def bin_search(li, element):
bottom = 0
top = len(li)-1
index = -1
while top>=bottom and index==-1:
mid = int([Link]((top+bottom)/2.0))
if li[mid]==element:
index = mid
elif li[mid]>element:
top = mid-1
else:
bottom = mid+1

return index

li=[2,5,7,9,11,17,222]
print(bin_search(li,11))
print(bin_search(li,12))
Question 8. Write a program which can compute the factorial of a given number.
Suppose the following input is supplied to the program: 8
Then, the output should be: 40320
def fact(x):
if x == 0:
return 1
return x * fact(x - 1)

x=int(input("Enter number: "))


print(fact(x))

Question 9: Write a program that prints all the numbers from 0 to 6 except 3
and 6.

for x in range(7):
# Check if the current value of 'x' is equal to 3 or 6
if (x == 3 or x == 6):
continue
print(x, end=' ')
print("\n")

Question 10: Count the number of even and odd numbers in a tuple. Tuple
includes numbers 1, 2, 3, 4, 5, 6, 7, 8, 9

numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
count_odd = 0
count_even = 0
for x in numbers:
if x % 2 == 0:
count_even += 1
else:
count_odd += 1
print("Number of even numbers:", count_even)
print("Number of odd numbers:", count_odd)

Question 11: Find the number of digits before comma in a floating number.
number = float(input("istediğiniz uzunlukta bir sayi
girin: "))
number_storage = number
digit1 = 0
while True:
print(number_storage)
if(number_storage >= 1):
number_storage = number_storage // 10
#virgülden önceki basamak adedini say
digit1 += 1
else:
break
print(digit1)
if (digit1 == 0):
digit1 = 1
## eğer 0.5 gibi bir sayi ise 1 basamak alınsın

print("virgülden önceki basamak adedi: ", digit1)

12. A = [1,2,3,4,5,6,7,8,9]
Liste içerisinde yer alan elemanları 2 birim kaydırın.
Oluşan yeni listenin elemanlarına erişip çift sayı olanlarının karesini alın ve bir
sözlük tipinde bunu kullanıcıya dönün.

A=[1,2,3,4,5,6,7,8,9]
new_A=A[6:8]+A[0:6]
print(new_A)
result = []
dict_new = {}
for i in new_A:
if i % 2==0:
[Link](i**2)
dict_new[i] = i**2
print(result)
print(dict_new)

You might also like