Skip to content

Commit 62bdb3d

Browse files
solutions to python assignments
1 parent 684fb5b commit 62bdb3d

9 files changed

+293
-0
lines changed

coin_tosses.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Write a function that simulates tossing a coin 5,000 times.
2+
Your function should print how many times the head/tail appears."""
3+
import random
4+
def coin_toss(tosses):
5+
heads = 0
6+
tails = 0
7+
for toss in range(tosses):
8+
num = random.random()
9+
if num < .5:
10+
heads += 1
11+
outcome = "heads"
12+
elif num >= .5:
13+
tails += 1
14+
outcome = "tails"
15+
print "Attempt #:",toss,"You flipped", outcome, "Total heads =",heads, " Total tails: ", tails
16+
17+
coin_toss(50)
18+
19+
20+

comparing_lists.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Write a program that compares two lists and prints a
2+
# message depending on if the inputs are identical or not.
3+
4+
# Your program should be able to accept and compare two lists:
5+
# list_one and list_two. If both lists are identical print
6+
# "The lists are the same". If they are not identical print
7+
# "The lists are not the same." Try the following test cases for
8+
# lists one and two:
9+
10+
def comp_lists(list_one, list_two):
11+
if list_one == list_two:
12+
print "The lists are the same."
13+
else:
14+
print "The lists are not the same."
15+
16+
# list_one = [1,2,5,6,2]
17+
# list_two = [1,2,5,6,2]
18+
19+
# list_one = [1,2,5,6,5]
20+
# list_two = [1,2,5,6,5,3]
21+
22+
# list_one = [1,2,5,6,5,16]
23+
# list_two = [1,2,5,6,5]
24+
25+
list_one = ['celery','carrots','bread','milk']
26+
list_two = ['celery','milk','bread','carrots']
27+
28+
comp_lists(list_one, list_two)

dict_basics.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Create a dictionary containing some information about yourself.
2+
The keys should include name, age, country of birth, favorite
3+
language.
4+
5+
Write a function that will print something like the following as
6+
it executes:
7+
8+
My name is Anna
9+
My age is 101
10+
My country of birth is The United States
11+
My favorite language is Python
12+
Copy
13+
There are two steps to this process, building a dictionary and
14+
then gathering all the data from it. Write a function that can
15+
take in and print out any dictionary keys and values.
16+
17+
Note: The majority of data we will manipulate as web developers
18+
will be hashed in a dictionary using key-value pairs. Repeat this
19+
assignment a few times to really get the hang of unpacking
20+
dictionaries,
21+
as it's a very common requirement of any web application."""
22+
23+
person1 = {
24+
"first_name": "Beth",
25+
"last_name": "Hart",
26+
"age": "old",
27+
"country": "United States",
28+
"language": "Python"
29+
}
30+
31+
def about_person(person):
32+
print "My name is " + person["first_name"]
33+
print "My age is " + person["age"]
34+
print "I was born in " + person["country"]
35+
print "My favorite language is " + person["language"]
36+
37+
about_person(person1)

filter_by_type.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Integer
2+
# If the integer is greater than or equal to 100, print "That's a big number!" If the integer is less than 100, print "That's a small number"
3+
4+
# String
5+
# If the string is greater than or equal to 50 characters print "Long sentence." If the string is shorter than 50 characters print "Short sentence."
6+
7+
# List
8+
# If the length of the list is greater than or equal to 10 print "Big list!" If the list has fewer than 10 values print "Short list."
9+
10+
def filter_type(el):
11+
if isinstance(el, int):
12+
print str(el), "is a number"
13+
if el >= 100:
14+
print "that's a big number"
15+
else:
16+
print "that's a small number"
17+
18+
if isinstance(el, str):
19+
print str(el), "is a string"
20+
if len(el) >= 50:
21+
print "long sentence"
22+
else:
23+
print "short sentence"
24+
if isinstance(el, list):
25+
print str(el), "is a list"
26+
if len(el) >= 10:
27+
print "that's a long list"
28+
else:
29+
print "that's a short list"
30+
31+
filter_type(45)
32+
filter_type(100)
33+
filter_type(455)
34+
filter_type(0)
35+
filter_type(-23)
36+
filter_type("Rubber baby buggy bumpers")
37+
filter_type("Experience is simply the name we give our mistakes")
38+
filter_type("Tell me and I forget. Teach me and I remember. Involve me and I learn.")
39+
filter_type("")
40+
filter_type([1,7,4,21])
41+
filter_type([3,5,7,34,3,2,113,65,8,89])
42+
filter_type([4,34,22,68,9,13,3,5,7,9,2,12,45,923])
43+
filter_type([])
44+
filter_type(['name','address','phone number','social security number'])

finding_chars.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Write a program that takes a list of strings
2+
# and a string containing a single character, and
3+
# prints a new list of all the strings containing
4+
# that character.
5+
6+
def find_char_words(char, list):
7+
char_words = []
8+
for word in list:
9+
if char in word:
10+
char_words.append(word)
11+
print char_words
12+
13+
14+
word_list = ['hello','world','my','name','is','Anna']
15+
char = 'o'
16+
find_char_words(char, word_list)

fun_w_functions.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#Create a function called odd_even that counts from 1
2+
# to 2000. As your loop executes have your program print
3+
# the number of that iteration and specify whether it's
4+
# an odd or even number.
5+
# def odd_even(min, max):
6+
# iteration = 1
7+
# for i in range (min, max+1):
8+
# if i %2 ==0:
9+
# num = "even"
10+
# else:
11+
# num = "odd"
12+
# print "Number is " + str(iteration) + ". The number is " + num + "."
13+
# iteration += 1
14+
# odd_even(1, 2000)
15+
16+
"""Create a function called 'multiply' that iterates through each
17+
value in a list (e.g. a = [2, 4, 10, 16]) and returns a list
18+
where each value has been multiplied by 5.
19+
The function should multiply each value in the list by the
20+
second argument."""
21+
22+
def multiply (list, multiple):
23+
multiplied = []
24+
for i in list:
25+
multiplied.append(i*multiple)
26+
return multiplied
27+
28+
a = [2, 4, 5]
29+
print multiply(a, 2)
30+
31+
'''Write a function that takes the multiply function call as an
32+
argument. Your new function should return the multiplied list as
33+
a two-dimensional list. Each internal list should contain the 1's
34+
times the number in the original list.'''
35+
36+
def layered_multiples(list, multiple):
37+
layered_list = []
38+
for i in list:
39+
inner_list = []
40+
for j in range(i*multiple):
41+
inner_list.append(1)
42+
layered_list.append(inner_list)
43+
return layered_list
44+
45+
print layered_multiples(multiply(a,2),3)
46+

mult_sums_avgs.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
#print odd from 1-1000
3+
for i in range(1, 1001, 2):
4+
print i
5+
6+
#multiples of 5 to 1,000,000
7+
multiple = 1
8+
result = 1
9+
while result < 1000000:
10+
result = 5*multiple
11+
multiple += 1
12+
print result
13+
14+
# sum list
15+
a = [1, 2, 5, 10, 255, 3]
16+
sum = 0
17+
for i in a:
18+
sum += i
19+
print sum
20+
21+
#print avg
22+
sum = 0
23+
for i in a:
24+
sum += i
25+
avg = sum/len(a)
26+
print avg
27+
28+

multi_tbl.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def create_multi_table(low, high):
2+
print "%3s" % "x", #uses min 3 spaces in % placeholder, then subs value after % into string
3+
for i in range(low,high+1):
4+
print "%3s" % i,
5+
count = low
6+
7+
while count < (high + low):
8+
print "\n%3s" % str(count),
9+
for i in range(low,high+1):
10+
print "%3s" % str(count*i),
11+
count += 1
12+
13+
create_multi_table(1,12)
14+

names.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''Given the following list:'''
2+
3+
students = [
4+
{'first_name': 'Michael', 'last_name' : 'Jordan'},
5+
{'first_name' : 'John', 'last_name' : 'Rosales'},
6+
{'first_name' : 'Mark', 'last_name' : 'Guillen'},
7+
{'first_name' : 'KB', 'last_name' : 'Tonel'}
8+
]
9+
10+
'''Create a program that outputs:
11+
12+
Michael Jordan
13+
John Rosales
14+
Mark Guillen
15+
KB Tonel'''
16+
17+
def student_roster(list):
18+
for item in list:
19+
print item["first_name"] + " " + item["last_name"]
20+
student_roster(students)
21+
22+
23+
24+
25+
'''Part II
26+
Now, given the following dictionary:'''
27+
28+
users = {
29+
'Students': [
30+
{'first_name': 'Michael', 'last_name' : 'Jordan'},
31+
{'first_name' : 'John', 'last_name' : 'Rosales'},
32+
{'first_name' : 'Mark', 'last_name' : 'Guillen'},
33+
{'first_name' : 'KB', 'last_name' : 'Tonel'}
34+
],
35+
'Instructors': [
36+
{'first_name' : 'Michael', 'last_name' : 'Choi'},
37+
{'first_name' : 'Martin', 'last_name' : 'Puryear'}
38+
]
39+
}
40+
'''Create a program that prints the following format (including number of characters in each combined name):
41+
Students
42+
1 - MICHAEL JORDAN - 13
43+
2 - JOHN ROSALES - 11
44+
3 - MARK GUILLEN - 11
45+
4 - KB TONEL - 7
46+
Instructors
47+
1 - MICHAEL CHOI - 11
48+
2 - MARTIN PURYEAR - 13'''
49+
50+
def students_formatted(dict):
51+
num = 1
52+
for key in dict:
53+
print key
54+
for value in dict[key]:
55+
full_cap_name = str.upper(value["first_name"]+ " " + value["last_name"])
56+
num_chars = len(value["first_name"] + value["last_name"])
57+
print str(num) + " - " + full_cap_name + " - " + str(num_chars)
58+
num += 1
59+
60+
students_formatted(users)

0 commit comments

Comments
 (0)