1. Define a function max()
that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Python. (It is true that Python has the max()
function built in, but writing it yourself is nevertheless a good exercise.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Program1 | |
def max (x,y): | |
if x>y: | |
return x | |
else: | |
return y | |
#Main program below | |
x=float(input("Write the first number")) | |
y=float(input("Write the second number")) | |
ans = max(x,y) | |
print("The maximum value is", ans) |
2. Define a function max_of_three()
that takes three numbers as arguments and returns the largest of them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Progam2 | |
def maximum_three (x, y, z): | |
return max(x, y, z) | |
#Main program below | |
x=float(input("Write the first number:")) | |
y=float(input("Write the second number:")) | |
z=float(input("Write the third number:")) | |
ma= maximum_three(x, y, z) | |
print("The largest value of the numbers you provide is", ma) |
3. Define a function that computes the length of a given list or string. (It is true that Python has the len()
function built in, but writing it yourself is nevertheless a good exercise.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Progam3 | |
def length(x): | |
counter = 0 | |
for c in x: | |
counter = counter + 1 | |
return counter | |
#Main program below | |
x=(input("Write your word")) | |
print("The number of characters of the word you provide is", length(x)) |
4. Write a function that takes a character (i.e. a string of length 1) and returns True
if it is a vowel, False
otherwise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Progam4 | |
def translate(x): | |
for c in x: | |
if c in ['a','e','i','o','u']: | |
return True | |
else: | |
return False | |
return answer | |
#Main program below | |
x=input("Enter a character:") | |
print(translate(x)) |
5. Write a function translate()
that will translate a text into “rövarsprĂ¥ket” (Swedish for “robber’s language”). That is, double every consonant and place an occurrence of "o"
in between. For example, translate("this is fun")
should return the string "tothohisos isos fofunon"
.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Progam5 | |
def translate(x): | |
answer = "" | |
for c in x: | |
if c in ['a','e','i','o','u', ' ']: | |
answer = answer + c | |
else: | |
answer = answer + c + "o" + c | |
return answer | |
#Main program below | |
x=input("Write a word:") | |
print(translate(x)) |
6. Define a function sum()
and a function multiply()
that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4])
should return 10
, and multiply([1, 2, 3, 4])
should return 24
.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Progam6 | |
def sum(list): | |
sum_answer = 0 | |
for i in range (sum_answer,len(list)): | |
sum_answer += list[i] | |
return sum_answer | |
def multiply(list1): | |
multiply_answer = 1 | |
for i in range (multiply_answer,len(list)): | |
multiply_answer *= list[i] | |
return multiply_answer | |
list = [1, 2, 3, 4] | |
#Main program below | |
print("The sum of the numbers in the list is", sum(list), ",and the result of multiplying the numbers in the list is", multiply(list)) |
7. Define a function reverse()
that computes the reversal of a string. For example, reverse("I am testing")
should return the string "gnitset ma I"
.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Progam7 | |
def reverse(x): | |
answer = "" | |
length = len(x) | |
counter = 0 | |
while counter < length: | |
answer = answer + x[length-counter-1] | |
counter = counter + 1 | |
return answer | |
#Main program below | |
x=str(input("Write something:")) | |
print(reverse(x)) |
8. Define a function is_palindrome()
that recognizes palindromes (i.e. words that look the same written backwards). For example, is_palindrome("radar")
should return True
.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Progam8 | |
def is_palindrome(x): | |
answer = "" | |
length = len(x) | |
counter = 0 | |
while counter < length: | |
answer = answer + x[length-counter-1] | |
counter = counter + 1 | |
return answer | |
#Main program below | |
x=str(input("Write something to prove if it is a palindrome:")) | |
if x == is_palindrome(x): | |
print("True") | |
else: | |
print("False") |
9. Write a function is_member()
that takes a value (i.e. a number, string, etc) x
and a list of values a
, and returns True
if x
is a member of a
, False
otherwise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Progam9 | |
def is_member(x): | |
x= input("Which is the value you want to check:") | |
ans=0 | |
for i in list: | |
if x == i: | |
ans += 1 | |
else: | |
ans += 0 | |
if ans >= 1: | |
return print("The value you provide is in the list.") | |
else: | |
return print("The value you provide is not in the list.") | |
#MainProgramBelow | |
list = [] | |
values= int(input("How many values you want in the list:")) | |
for i in range(values): | |
num = input("Write a value: ") | |
list.append(num) | |
y = is_member(list) |
10. Define a function overlapping()
that takes two lists and returns True if they have at least one member in common, False otherwise. You may use your is_member()
function, or the in
operator, but for the sake of the exercise, you should (also) write it using two nested for-loops.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Progam10 | |
def overlapping(list1, list2): | |
answer = False | |
for i in list1: | |
for j in list2: | |
if j == i: | |
answer = True | |
return answer | |
#MainProgramBelow | |
list1 = [] | |
values= int(input("How many values you want in the first list:")) | |
for i in range(values): | |
num = input("Write a value: ") | |
list1.append(num) | |
list2 = [] | |
values= int(input("How many values you want in the second list:")) | |
for i in range(values): | |
num = input("Write a value: ") | |
list2.append(num) | |
print (overlapping(list1,list2)) |