0% found this document useful (0 votes)
4 views6 pages

CS331 Lab2

Uploaded by

arsany Samir
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)
4 views6 pages

CS331 Lab2

Uploaded by

arsany Samir
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
You are on page 1/ 6

Python - Sheet 2

Problem 1
Assume we have the following dictionary:

fruitToColor = { 'banana' : 'yellow' ,

'blueberry' : 'blue' ,

'cherry' : 'red' ,

'lemon ' : 'yellow' ,

' kiwi' : 'green' ,

'strawberry ' : 'red' ,

'tomato' : 'red' }

Write a program that creates a dictionary that maps colors to lists of fruits that have this color,
e.g. color to fruits['yellow'] should produce ['banana','lemon'].

Hint: Before you can add an item to a list it has to be initialized. This has to be done the first
time you add any fruit of a color (i.e. when color to fruits does not yet have a key for this color).

Problem 2

Write a program computing the greatest common divisor (GCD) of two given integers, using the
Euclidean Algorithm. You are encouraged to implement both the recursive and non-recursive
versions.

The input consists of several lines, with each line containing two positive integers, a and
b, separated by a white space (a; b < 10000). A pair of terminates the input. The output should
have one GCD per line for each pair

Sample Input:
10 15

55 14

Sample Output:
5

1
Problem 3

Write a Python function called letterFrequency() that, given an input string, calculates the
frequency of each letter (converted to lowercase) in relation to the total number of letters
encountered.

Sample Input:
Hello

Sample Output:
Total letters: 5

h: 0.2

e: 0.2

l: 0.4

o: 0.2
Answer Sheet2
Problem 1
fruitToColor =
{'banana':'yellow','blueberry':'blue','chery':'red,
'lemon':'yellow','kiwi':'green',
'strawberry':'red','tomato':'red'};
color=raw_input('Enter the fruit color:');
fruits=[]
for key in fruitToColor:
if(fruitToColor[key]==color):
fruits.append(key);
fruits.reverse();
print(fruits);

Problem 2
Recursive version:
def gcd(m,n):
if(m%n==0):
return n;
else:
return gcd(n,m%n);
Non-recursive version:
def gcd(m,n):
if(m<n):
m+=n;
n=m-n;
m-=n;
if(n==0):
return m;
while(m%n!=0):
m+=n;
n=m-n;
m-=n;
n%=m;
return n;
Test program:
print('Enter Numbers');
i=0;
lines=[]
x=y=z='';
while i<2:
lines.append(raw_input());
i+=1;
for line in lines:
for digit in line:
if( digit!=' '):
z+=digit;
elif(digit==' '):
x=int(z);
z='';
continue;
if(digit==line[len(line)-1]):
y=int(z);
print(gcd(x,y));
middle school
Problem 3
from __future__ import division
from string import lower
def letterfrequncy(str):
str=lower(str);
letters=[];
frequencies=[];
print'Total Letters= ',len(str);
for letter in str:
count=str.count(letter);
if(letter not in letters):
letters.append(letter);

frequencies.append(count/len(str));
x=0
while x<len(letters):
print letters[x],':',frequencies[x];
x+=1;
str=raw_input('Enter the string:');
letterfrequncy(str);

You might also like