Monday, 12 October 2020

What is Data Programing?



BDAT 1004 – Data Programming

Problem Set 1 

This problem set is based on lectures 1,2 and 3. For a complete list of topics please consult page 2 of the course syllabus. Please consult the “Instructions for Problem Set Submissions” document under course information before submitting your assignment.

Question 1 
What data type is each of the following?


5

5.0

5 > 1

'5'

5 * 2

'5' * 2

'5' + '2'

5 / 2

5 // 2

[5, 2, 1]

5 in [1, 4, 6]

math.pi

 

Question 2 Write (and evaluate) Python expressions that answer these questions: a. How many letters are there in 'Supercalifragilisticexpialidocious'? b. Does 'Supercalifragilisticexpialidocious' contain 'ice' as a substring?

c. Which of the following words is the longest: Supercalifragilisticexpialidocious, Honorificabilitudinitatibus, or Bababadalgharaghtakamminarronnkonn? 

d. Which composer comes first in the dictionary: 'Berlioz', 'Borodin', 'Brian', 'Bartok', 'Bellini', 'Buxtehude', 'Bernstein'. Which one comes last? 


Question 3 
a. Write a function inside(x,y,x1,y1,x2,y2) that returns True or False depending on whether the point (x,y) lies in the rectangle with lower left corner (x1,y1) and upper right corner (x2,y2).

>>> inside(1,1,0,0,2,3)
 True 
>>> inside(-1,-1,0,0,2,3) False 

b. Use function inside() from part a. to write an expression that tests whether the point (1,1) lies in both of the following rectangles: one with lower left corner (0.3, 0.5) and upper right corner (1.1, 0.7) and the other with lower left corner (0.5, 0.2) and upper right corner (1.1, 2).

Question 4 
16. You can turn a word into pig-Latin using the following two rules (simplified): • If the word starts with a consonant, move that letter to the end and append 'ay'. For example, 'happy' becomes 'appyhay' and 'pencil' becomes 'encilpay'. • If the word starts with a vowel, simply append 'way' to the end of the word. For example, 'enter' becomes 'enterway' and 'other' becomes 'otherway' . For our purposes, there are 5 vowels: a, e, i, o, u (so we count y as a consonant). Write a function pig() that takes a word (i.e., a string) as input and returns its pigLatin form. Your function should still work if the input word contains upper case characters. Your output should always be lower case however.  

>>> pig('happy') 'appyhay

>>> pig('Enter') 
'enterway' 

Question 5 File bloodtype1.txt records blood-types of patients (A, B, AB, O or OO) at a clinic. Write a function bldcount() that reads the file with name name and reports (i.e., prints) how many patients there are in each bloodtype.

>>> bldcount('bloodtype.txt') 
There are 10 patients of blood type A.
 There is one patient of blood type B. 
There are 10 patients of blood type AB. 
There are 12 patients of blood type O. 
There are no patients of blood type OO.  

Question 6 
Write a function curconv() that takes as input: 

1. a currency represented using a string (e.g., 'JPY' for the Japanese Yen or 'EUR' for the Euro) 
2. an amount

and then converts and returns the amount in US dollars.

>>> curconv('EUR', 100)

122.96544 

>>> curconv('JPY', 100) 

1.241401

The currency rates you will need are stored in file currencies.txt:

AUD 1.0345157 Australian Dollar 
CHF 1.0237414 Swiss Franc 
CNY 0.1550176 Chinese Yuan 
DKK 0.1651442 Danish Krone 
EUR 1.2296544 Euro 
GBP 1.5550989 British Pound 
HKD 0.1270207 Hong Kong Dollar 
INR 0.0177643 Indian Rupee 
JPY 0.01241401 Japanese Yen 
MXN 0.0751848 Mexican Peso 
MYR 0.3145411 Malaysian Ringgit 
NOK 0.1677063 Norwegian Krone 
NZD 0.8003591 New Zealand Dollar 
PHP 0.0233234 Philippine Peso 
SEK 0.148269 Swedish Krona 
SGD 0.788871 Singapore Dollar 
THB 0.0313789 Thai Baht  


Question 7 Each of the following will cause an exception (an error). Identify what type of exception each will cause.


Trying to add incompatible variables, as in

adding 6 + ‘a’

 

Referring to the 12th item of a list that has only 10

items

 

Using a value that is out of range for a function’s

 input, such as calling math.sqrt(-1.0)

 

Using an undeclared variable, such as print(x) when x

 has not been defined

 


 
Trying to open a file that does not exist, such as 
mistyping the file name or looking in the 
wrong directory.  

Question 8 Encryption is the process of hiding the meaning of a text by substituting letters in the message with other letters, according to some system. If the process is successful, no one but the intended recipient can understand the encrypted message. Cryptanalysis refers to attempts to undo the encryption, even if some details of the encryption are unknown (for example, if an encrypted message has been intercepted). The first step of cryptanalysis is often to build up a table of letter frequencies in the encrypted text. Assume that the string letters is already defined as 'abcdefghijklmnopqrstuvwxyz'. Write a function called frequencies() that takes a string as its only parameter, and returns a list of integers, showing the number of times each character appears in the text. Your function may ignore any characters that are not in letters.


>>> frequencies('The quick red fox got bored and went home.') [1, 1, 1, 3, 5, 1, 1, 2, 1, 0, 1, 0, 1, 2, 4, 0, 1, 2, 0, 2, 1, 0, 1, 1, 0, 0] >>> frequencies('apple')


Question 9 The Sieve of Erastophenes is an algorithm -- known to ancient Greeks -- that finds all prime numbers up to a given number n. It does this by first creating a list L from 2 to n and an (initially empty) list primeL. The algorithm then takes the first number in list L (2) and appends it to list primeL, and then removes 2 and all its multiples (4,6,8,10,12, ...) from L. The algorithm then takes the new first number in L (3) and appends it to list primeL, and then removes from L 3 and all its remaining multiples (9,15,21,...). So, in every iteration, the first number of list L is appended to list primeL and then it and its multiples are removed from list L. The iterations stop when list L becomes empty. Write a function sieve() that takes as input a positive integer n, implements the above algorithm, and returns a list of all prime numbers up to n.


>>> sieve(56) 
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53] 
>>> sieve(368) 
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367] >>> sieve(32) 
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] 

Question 10 Implement function triangleArea(a,b,c) that takes as input the lengths of the 3 sides of a triangle and returns the area of the triangle. By Heron's formula, the area of a triangle with side lengths a, b, and c is s(s - a)(s -b)(s -c) , where s = (a+b+c)/2.

>>> triangleArea(2,2,2) 1.7320508075688772

 
 

UK assignment helper

Author & Editor

We are the best assignment writing service provider in the UK. We can say it with pride that we tend to perceive our client’s requirements better than any other company. We provide assignment writing service in 100+ subjects.

0 comments:

Post a Comment