Skip to content

Instantly share code, notes, and snippets.

View LizaPiya's full-sized avatar

Fahmida Liza Piya LizaPiya

  • University of Delaware
  • Dhaka
View GitHub Profile
@LizaPiya
LizaPiya / Project 1-lizapiya-Probem1
Last active September 18, 2023 14:51
Python function,files and dictionaries
##To start, define a function called strip_punctuation which takes one parameter, a string which represents a word, and removes characters considered punctuation from everywhere in the word. (Hint: remember the .replace() method for strings.)
punctuation_chars = ["'", '"', ",", ".", "!", ":", ";", '#', '@']
def strip_punctuation(word):
New_word=""
print("Word is: ",word)
for w in word:
if w not in punctuation_chars:
Exercise 1
states = {"Minnesota": ["St. Paul", "Minneapolis", "Saint Cloud", "Stillwater"],
"Michigan": ["Ann Arbor", "Traverse City", "Lansing", "Kalamazoo"],
"Washington": ["Seattle", "Tacoma", "Olympia", "Vancouver"]}
print(sorted(states, key=lambda state: len(states[state][0])))
--------------------------------------------------------------------------
Exercise 2
## Create a function called mult that has two parameters, the first is required and should be an integer, the second is an optional parameter that can either be a number or a string but whose default is 6. The function should return the first parameter multiplied by the second.
def mult(a,b=6):
return a*b
## The .items() method produces a sequence of key-value pair tuples. With this in mind, write code to create a list of keys from the dictionary track_medal_counts and assign the list to the variable name track_events. Do NOT use the .keys() method.
track_medal_counts = {'shot put': 1, 'long jump': 3, '100 meters': 2, '400 meters': 2, '100 meter hurdles': 3, 'triple jump': 3, 'steeplechase': 2, '1500 meters': 1, '5K': 0, '10K': 0, 'marathon': 0, '200 meters': 0, '400 meter hurdles': 0, 'high jump': 1}
track_keys=[]
for i in track_medal_counts.items():
track_keys.append(i[0])
track_events=track_keys
## Write two functions, one called addit and one called mult. addit takes one number as an input and adds 5. mult takes one number as an input, and multiplies that input by whatever is returned by addit, and then returns the result.
def addit(a):
add = a+5
return add
def mult(a):
return a*addit(a)
addit(5)
## Write a function named same that takes a string as input, and simply returns that string.
def same (string):
return string
The dictionary Junior shows a schedule for a junior year semester. The key is the course name and the value is the number of credits. Find the total number of credits taken this semester and assign it to the variable credits. Do not hardcode this – use dictionary accumulation!
Junior = {'SI 206':4, 'SI 310':4, 'BL 300':3, 'TO 313':3, 'BCOM 350':1, 'MO 300':3}
credits=0
for credit in Junior:
credits=credits+Junior[credit]
## 1. Create a dictionary called d that keeps track of all the characters in the string placement and notes how many times each character was seen. Then, find the key with the lowest value in this dictionary and assign that key to min_value.
placement = "Beaches are cool places to visit in spring however the Mackinaw Bridge is near. Most people visit Mackinaw later since the island is a cool place to explore."
d={}
for i in placement:
if i not in d:
d[i]=0
d[i]=d[i]+1
print (d)
ks=list(d.keys())
## The dictionary travel contains the number of countries within each continent that Jackie has traveled to. Find the total number of countries that Jackie has been to, and save this number to the variable name total. Do not hard code this!
travel = {"North America": 2, "Europe": 8, "South America": 3, "Asia": 4, "Africa":1, "Antarctica": 0, "Australia": 1}
total=0
for i in travel:
total=total+travel[i]
## If we want to find out how often the letter ‘t’ occurs, we can accumulate the result in a count variable.
f = open('scarlet.txt', 'r')
txt = f.read()
# now txt is one long string containing all the characters
t_count = 0 #initialize the accumulator variable
for c in txt:
if c == 't':
t_count = t_count + 1 #increment the counter
print("t: " + str(t_count) + " occurrences")