C200 Programming Assignment №5 Unboundled Loops Professor M.M. Dalkilic Edited by Geoffrey Brown Computer Science School of Informatics, Computing, and Engineering Indiana University, Bloomington, IN,...

1 answer below »
I need help with my python homework as I attached it in the form of a PDF. There are six problems in this PDF. I also attached the workspace where I need to write the code as shown in the form of a PDF.Reminder: Please follow the instruction in the PDF closely. DO NOT use too complicated functions to solve problems because this is the course for beginners.


C200 Programming Assignment №5 Unboundled Loops Professor M.M. Dalkilic Edited by Geoffrey Brown Computer Science School of Informatics, Computing, and Engineering Indiana University, Bloomington, IN, USA September 28, 2021 Contents Introduction 2 All the Deliverables for Homework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Problem 1: Entropy 2 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Problem 2: Magic in Programming! 4 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Problem 3: Magic Square 6 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Problem 4: Caeser Cipher 9 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Problem 5: Radix 11 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Problem 6: Central Dogma 13 Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Assignment №5 Bases, Files, Loops Page 1 Introduction All the Deliverables for Homework Complete the functions described below. You will be asked to rewrite some code and design and implement others. This homework is meant to help you improve your understanding of loops, bases, file I/O using slightly more complex problems. Remember, problem→ conceptualization → logic→ implementation with bugs→ solution and interpretation. You will complete this before Tuesday, October 5 2021 11PM (Bloomington, IN). You will not turn anything in on canvas. If your timestamp is 11:00PM or greater, the homework cannot be graded. So do not wait until 10:58PM to turn it in. This homework asks you to start creating your own functions to implement the required ones. This allows you more creativity. Since we have an impending exam (next homework), we have decreased the number of problems. We will not be accepting late homework. Problem 1: Entropy In this problem, we calculate what’s called Entropy. This is related to the entropy you’ve seen in physics and chemistry. Entropy is used in AI and ML. Some time ago, Claude Shannon at IBM was working on making a mathematical model of communication. Imagine, you’d have a piece of text, and then his function would give a number indicating how important or significant the message was. What Shannon observed was that importance was really the degree of how surprising the message was. So, in a sense, the message itself wasn’t as important as how unexpected it was. He then realized that he could utilize probability as a measure of surprise: Message Entropy Probability not surprising ↓ high surprising ↑ low For this problem, we are only working with finite probabilities. We can think of probabilities as a list of numbers p0, p1, . . . , pn such that pi ≥ 0, i = 0, 1, . . . , n (1) 1 = p0 + p1 + p2 + · · ·+ pn (2) = n∑ i=0 pi (3) Line (3) is usually how it’s written using the ∑ as a shorthand for addition. We’ll learn about this later, but I thought it might be interesting to see it now. Observe it looks a lot like range! You might think about what’s similar and what’s different. To say in words, they are a finite collection of numbers that are non-negative that sum to exactly one. We can make a list (we’ll assume the items are of the same type and immutable) into a probability. Consider x = ["a","b","a","c","c","a"] Assignment №5 Bases, Files, Loops Page 2 1. gather the items uniquely 2. count each time the item occurs 3. find the total count 4. create a new list of the ratio of count to total For x, we have: 1. {"a": 3, "b":1, "c": 2} 2. total count is 6 3. return [3/6,1/6,2/6] (Please note: the fractions here are for understanding but not expecting output like that in your code) We still need to show you how to calculate entropy: entropy = −(p0 log2(p0) + p1 log2(p1) + · · ·+ pn log2(pn) (4) entropy(makeProbability(x)) = −(3 6 log2(3/6) + 1 6 log2(1/6) + 2 6 log2(2/6) (5) = −(.5(−1.0) + 0.17(−2.58) + .33(−1.58)) (6) = 1.46 (7) Because of continuity arguments we treat log2(0) = 0. Python’s math module will correctly state this math error, so you’ll have to simply add 0 if the probability is 0. Deliverables Problem 1 • Complete log_2 function that returns the log2 of the input • Complete makeProbability which takes a list of immutable objects and returns a probability distribution: a list of values [p0, p1, . . . , pn] such that: pi ≥ 0 for i = 0, 1, . . . , n∑n i=0 pi = p0 + p1 + · · ·+ pn = 1 • Complete entropy function • Provide docstrings for all the functions Assignment №5 Bases, Files, Loops Page 3 Problem 2: Magick You’ve encountered a soothsayer named Soothy McSoothface. He will magically determine any whole, positive number you guess by asking you to perform a few operations and tell him the result. It’s quite amazing. For example, he asks you your age (say you’re 25). He then asks you to add fifteen, multiply by three, subtract nine, divide by 3 and tell him the number. You say, “It’s 37.” He replies, “Your age is...is twenty-five!” You are skeptical and implement this as a Python program. Magical Encantation Pick a number x Add fifteen to x Multiply the sum by three Subtract nine from the product Divide the difference by three Subtract 12 from the quotient Hocus Pocus–that is your number Table 1: Encantation (operations) on a secret number that yields the secret number at the end. Ponder this a bit, and try your best. When you’re done, and it works, take a look on the next page. To help you out, we’ll remind you of some of the vocabulary: Operation Result Name Division Quotient Addition Sum Multiplication Product Subtraction Difference Power Exponetiation Table 2: Vocabulary for mathematical operations. Assignment №5 Bases, Files, Loops Page 4 Deliverables Problem 2 • Complete the function magick • What does the function do? • There is not any unit testing, since this would give the function away... • Provide docstring for the function. Assignment №5 Bases, Files, Loops Page 5 Problem 3: Magic Square Please visit https://en.wikipedia.org/wiki/Magic_square. You’ll learn about magic squares. In this problem, you’ll write a Boolean function that returns True if the 3 × 3 square is magic, and False otherwise. For the problem here all squares are size three. In this problem, we are giving you the main function, but it is up to you how to add additional functions to make the problem easier to solve. The unit testing will only examine is_magic_square and generate_3_square. Generating a Magic Square We can use brute force to generate all magic squares of size three. If we look at the solutions as a simple list of numbers, we see they are permutations–unique reorderings. For example, the square below 8 1 6 3 5 7 4 9 2 can be thought of as the sequence. If we had a way to generate all the different permutations we could, if it was small enough, check for magic squares. As it turns out, for a list of n objects, there are n! (read n factorial) permutations. We can find how many using Python: 1 >>> math . f a c t o r i a l ( 9 ) 2 362880 3 >>> Writing our own permutation function is for homework in a few weeks. For now, we can use a module in Python, itertools.permutations, shown here: 1 >>> import i t e r t o o l s 2 >>> p = i t e r t o o l s . permutat ions ( [ 1 , 2 , 3 ] ) 3 >>> fo r i in p : 4 . . . p r i n t ( i ) 5 . . . 6 (1
Answered Same DayOct 01, 2021

Answer To: C200 Programming Assignment №5 Unboundled Loops Professor M.M. Dalkilic Edited by Geoffrey Brown...

Dinesh answered on Oct 02 2021
136 Votes
documents/assignment-5-3n4l1sdf.pdf
import math
import itertools
##########################################################################
#PROBLEM 1
##########################################################################
#
#INPUT positive number n
#RETURN log of number base 2
def log_2(n):
pass
#INPUT list of immutable objects
#RETURN probability distribution
def makeProbability(xlst):
pass
#INPUT probability distribution
#RETURN non-negative number entropy
def entropy(xlst):
pass
##########################################################################
#PROBLEM 2
##########################################################################
#INPUT positive integer
#RETURN positive integer
def magick(x):
pass
##########################################################################
#PROBLEM 3
##########################################################################
#INPUT a list of lists of three positive integers [[a,b,c],[d,e,f],[g,h,i]]
#RETURN True if the input is a magic square
#You can create other functions to help you--they will
#not be unit tested
def is_magic_square(s3):

pass
#INPUT nothing
#RETURN list of solutions to magic square size 3
def generate_3_square():
pass
##########################################################################
# PROBLEM 4
##########################################################################
#INPUT takes a letter and shift
#RETURN new letter shifted
def encrypt(letter, n):
pass
#INPUT takes a letter and shift
#RETURN original letter
def decrypt(letter, n):
pass
#INPUT takes a sentence of lowercase letters and spaces and shift
#RETURN caeser cypher
def encrypt_sentence(sentence, shift):
pass
#INPUT takes an encrypted sentence and shift
#RETURN decrypted sentence
def decrypt_sentence(sentence, shift):
pass
##########################################################################
# PROBLEM 5
##########################################################################
#INPUT non-negative integer and non-negative integer > 1
#RETURN Wild Number [string, base]
#string is encoding of number in base, base is integer
def make_number(decimal, base):
pass
#INPUT Wild number
#RETURN new wild number in new base
def convert(number, base):
pass
#INPUT two wild numbers
#RETURN product as a (possibly new) base
def mul_(number1, number2, base):
pass
#INPUT two wild numbers
#RETURN sum as a (possibly new) base
def add_(number1, number2, base):
pass
protein =
translate(DNA_d)#######################################################################
# Problem 6
#INPUT path to amino acid file
#RETURN a dictionary
#Key is a tuple (c0, c1, ... , cn) where ci are codons
#Value is a pair [name, abbreviation] for the amino acid
def get_amino_acids(file_path):
pass
#INPUT path to DNA file
#RETURN a list [header, DNA]
#header is first line in the file
#DNA is a string of letters from remainder of file
#no whitespace
def get_DNA(file_path):
pass
#INPUT FAST file
#RETURN a string representing the protein
#using the dictionary
def translate(DNA_d):
pass
documents/homework5fall2021-vwd3warq.pdf
C200 Programming Assignment №5
Unboundled Loops
Professor M.M. Dalkilic
Edited by Geoffrey Brown
Computer Science
School of Informatics, Computing, and Engineering
Indiana University, Bloomington, IN, USA
September 28, 2021
Contents
Introduction 2
All the Deliverables for Homework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Problem 1: Entropy 2
Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Problem 2: Magic in Programming! 4
Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Problem 3: Magic Square 6
Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Problem 4: Caeser Cipher 9
Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Problem 5: Radix 11
Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Problem 6: Central Dogma 13
Deliverables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Assignment №5 Bases, Files, Loops Page 1
Introduction
All the Deliverables for Homework
Complete the functions described below. You will be asked to rewrite some code and design and
implement others. This homework is meant to help you improve your understanding of loops,
bases, file I/O using slightly more complex problems. Remember, problem→ conceptualization
→ logic→ implementation with bugs→ solution and interpretation. You will complete this before
Tuesday, October 5 2021 11PM (Bloomington, IN). You will not turn anything in on canvas. If
your timestamp is 11:00PM or greater, the homework cannot be graded. So do not wait until
10:58PM to turn it in. This homework asks you to start creating your own functions to implement
the required ones. This allows you more creativity. Since we have an impending exam (next
homework), we have decreased the number of problems.
We will not be accepting late homework.
Problem 1: Entropy
In this problem, we calculate what’s called Entropy. This is related to the entropy you’ve seen
in physics and chemistry. Entropy is used in AI and ML. Some time ago, Claude Shannon at
IBM was working on making a mathematical model of communication. Imagine, you’d have a
piece of text, and then his function would give a number indicating how important or significant
the message was.
What Shannon observed was that importance was really the degree of how surprising the
message was. So, in a sense, the message itself wasn’t as important as how unexpected it
was. He then realized that he could utilize probability as a measure of surprise:
Message Entropy Probability
not surprising ↓ high
surprising ↑ low
For this problem, we are only working with finite probabilities. We can think of probabilities
as a list of numbers p0, p1, . . . , pn such that
pi ≥ 0, i = 0, 1, . . . , n (1)
1 = p0 + p1 + p2 + · · ·+ pn (2)
=
n∑
i=0
pi (3)
Line (3) is usually how it’s written using the

as a shorthand for addition. We’ll learn about this
later, but I thought it might be interesting to see it now. Observe it looks a lot like range! You
might think about what’s similar and what’s different. To say in words, they are a finite collection
of numbers that are non-negative that sum to exactly one.
We can make a list (we’ll assume the items are of the same type and immutable) into a
probability. Consider x = ["a","b","a","c","c","a"]
Assignment №5 Bases, Files, Loops Page 2
1. gather the items uniquely
2. count each time the item occurs
3. find the total count
4. create a new list of the ratio of count to total
For x, we have:
1. {"a": 3, "b":1, "c": 2}
2. total count is 6
3. return [3/6,1/6,2/6] (Please note: the fractions here are for understanding but not
expecting output like that in your code)
We still need to show you how to calculate entropy:
entropy = −(p0 log2(p0) + p1 log2(p1) + · · ·+ pn log2(pn) (4)
entropy(makeProbability(x)) = −(3
6
log2(3/6) +
1
6
log2(1/6) +
2
6
log2(2/6) (5)
= −(.5(−1.0) + 0.17(−2.58) + .33(−1.58)) (6)
= 1.46 (7)
Because of continuity arguments we treat log2(0) = 0. Python’s math module will correctly state
this math error, so you’ll have to simply add 0 if the probability is 0.
Deliverables Problem 1
• Complete log_2 function that returns the log2 of the input
• Complete makeProbability which takes a list of immutable objects and returns a
probability distribution: a list of values [p0, p1, . . . , pn] such that:
pi ≥ 0 for i = 0, 1, . . . , n∑n
i=0 pi = p0 + p1 + · · ·+ pn = 1
• Complete entropy function
• Provide docstrings for all the functions
Assignment №5 Bases, Files, Loops Page 3
Problem 2: Magick
You’ve encountered a soothsayer named Soothy McSoothface. He will magically determine any
whole, positive number you guess by asking you to perform a few operations and tell him the
result. It’s quite amazing. For example, he asks you your age (say you’re 25). He then asks you
to add fifteen, multiply by three, subtract nine, divide by 3 and tell him the number.
You say, “It’s 37.”
He replies, “Your age is...is twenty-five!”
You are skeptical and implement this as a Python program.
Magical Encantation
Pick a number x
Add fifteen to x
Multiply the sum by three
Subtract nine from the product
Divide the difference by three
Subtract 12 from the quotient
Hocus Pocus–that is your number
Table 1: Encantation (operations) on a secret number that yields the secret number at the end.
Ponder this a bit, and try your best. When you’re done, and it works, take a look on the next
page. To help you out, we’ll remind you of some of the vocabulary:
Operation Result Name
Division Quotient
Addition Sum
Multiplication Product
Subtraction Difference
Power Exponetiation
Table 2: Vocabulary for mathematical operations.
Assignment №5 Bases, Files, Loops Page 4
Deliverables Problem 2
• Complete the function magick
• What does the function do?
• There is not any unit testing, since this would give the function away...
• Provide docstring for the function.
Assignment №5 Bases, Files, Loops Page 5
Problem 3: Magic Square
Please visit https://en.wikipedia.org/wiki/Magic_square. You’ll learn about magic squares. In
this problem, you’ll write a Boolean function that returns True if the 3 × 3 square is magic, and
False otherwise. For the problem here all squares are size three. In this problem, we are giving
you the main function, but it is up to you how to add additional functions to make the problem
easier to solve. The unit testing will only examine is_magic_square and generate_3_square.
Generating a Magic Square
We can use brute force to generate all magic squares of size three. If we look at the solutions
as a simple list of numbers, we see they are permutations–unique reorderings. For example,
the square below
8 1 6
3 5 7
4 9 2
can be thought of as the sequence. If we had a way to generate all the different permutations
we could, if it was small enough, check for magic squares. As it turns out, for a list of n objects,
there are n! (read n factorial) permutations. We can find how many using Python:
1 >>> math . f a c t o r i a l ( 9 )
2 362880
3 >>>
Writing our own permutation function is for homework in a few weeks. For now, we can use a
module in Python, itertools.permutations, shown here:
1 >>> import i t e r t o o l s
2 >>> p = i t e r t o o l s . permutat ions ( [ 1 , 2 , 3 ] )
3 >>> fo r i in p :
4 . . . p r i n t ( i )
5 . . .
6 (1 , 2 , 3)
7 (1 , 3 , 2)
8 (2 , 1 , 3)
9 (2 , 3 , 1)
10 (3 , 1 , 2)
11 (3 , 2 , 1)
12 >>> p = i t e r t o o l s . permutat ions ( "123" )
13 >>> fo r i in p :
14 . . . p r i n t ( i )
15 . . .
Assignment №5 Bases, Files, Loops Page 6
16 ( ’ 1 ’ , ’ 2 ’ , ’ 3 ’ )
17 ( ’ 1 ’ , ’ 3 ’ , ’ 2 ’ )
18 ( ’ 2 ’ , ’ 1 ’ , ’ 3 ’ )
19 ( ’ 2 ’ , ’ 3 ’ , ’ 1 ’ )
20 ( ’ 3 ’ , ’ 1 ’ , ’ 2 ’ )
21 ( ’ 3 ’ , ’ 2 ’ , ’ 1 ’ )
22 >>>
As you can see, it takes an iterable and returns a list of tuples of all the permutations.
solution = 8, 1, 6, 3, 5, 7, 4, 9, 2
The function generate_3_squares returns a list of squares. When run, it should return:
1 [ [ [ 2 , 7 , 6 ] , [ 9 , 5 , 1 ] , [ 4 , 3 , 8 ] ] ,
2 [ [ 2 , 9 , 4 ] , [ 7 , 5 , 3 ] , [ 6 , 1 , 8 ] ] ,
3 [ [ 4 , 3 , 8 ] , [ 9 , 5 , 1 ] , [ 2 , 7 , 6 ] ] ,
4 [ [ 4 , 9 , 2 ] , [ 3 , 5 , 7 ] , [ 8 , 1...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here