Skip to content

Instantly share code, notes, and snippets.

View mayankdawar's full-sized avatar

Mayank Dawar mayankdawar

  • Ludhiana , Chandigarh , Mohali
View GitHub Profile
@mayankdawar
mayankdawar / SortFunc.py
Created February 22, 2020 19:18
Write code to rearrange the strings in the list winners so that they are in alphabetical order from A to Z.
winners = ['Kazuo Ishiguro', 'Rainer Weiss', 'Youyou Tu', 'Malala Yousafzai', 'Alice Munro', 'Alvin E. Roth']
winners.sort()
@mayankdawar
mayankdawar / removefunc.py
Created February 22, 2020 18:55
Write code to take ‘London’ out of the list trav_dest.
trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'London', 'Melbourne']
trav_dest.remove('London')
@mayankdawar
mayankdawar / vowelInString.py
Created February 19, 2020 18:49
Write code that will count the number of vowels in the sentence s and assign the result to the variable num_vowels. For this problem, vowels are only a, e, i, o, and u. Hint: use the in operator with vowels.
s = "singing in the rain and playing in the rain are two entirely different situations but both can be fun"
vowels = ['a','e','i','o','u']
count = 0
# Write your code here.
for i in s:
if i in vowels:
count += 1
num_vowels = count
@mayankdawar
mayankdawar / charInString.py
Created February 19, 2020 18:44
Write code that counts the number of words in sentence that contain either an “a” or an “e”. Store the result in the variable num_a_or_e. Note 1: be sure to not double-count words that contain both an a and an e. HINT 1: Use the in operator. HINT 2: You can either use or or elif. Hard-coded answers will receive no credit.
sentence = "python is a high level general purpose programming language that can be applied to many different classes of problems."
sen = sentence.split()
count = 0
for i in sen:
if 'a' in i or 'e' in i:
count += 1
num_a_or_e = count
@mayankdawar
mayankdawar / countW.py
Created February 19, 2020 18:41
Write code to count the number of strings in list items that have the character w in it. Assign that number to the variable acc_num. HINT 1: Use the accumulation pattern! HINT 2: the in operator checks whether a substring is present in a string. Hard-coded answers will receive no credit.
items = ["whirring", "wow!", "calendar", "wry", "glass", "", "llama","tumultuous","owing"]
count = 0
for i in items:
if 'w' in i:
count += 1
acc_num = count
@mayankdawar
mayankdawar / rainyMonths.py
Created February 19, 2020 18:33
rainfall_mi is a string that contains the average number of inches of rainfall in Michigan for every month (in inches) with every month separated by a comma. Write code to compute the number of months that have more than 3 inches of rainfall. Store the result in the variable num_rainy_months. In other words, count the number of items with values…
rainfall_mi = "1.65, 1.46, 2.05, 3.03, 3.35, 3.46, 2.83, 3.23, 3.5, 2.52, 2.8, 1.85"
months = rainfall_mi.split(",")
count = 0
for i in months:
if float(i) > 3.0:
count += 1
num_rainy_months = count
@mayankdawar
mayankdawar / accumConditionalPattren_2.py
Created February 19, 2020 17:54
Challenge For each word in words, add ‘d’ to the end of the word if the word ends in “e” to make it past tense. Otherwise, add ‘ed’ to make it past tense. Save these past tense words to a list called past_tense.
words = ["adopt", "bake", "beam", "confide", "grill", "plant", "time", "wave", "wish"]
past_tense = []
for i in words:
if(i[len(i)-1] == 'e'):
i += 'd'
else:
i += 'ed'
past_tense.append(i)
@mayankdawar
mayankdawar / accumConditionalPattren.py
Created February 19, 2020 17:47
For each string in the list words, find the number of characters in the string. If the number of characters in the string is greater than 3, add 1 to the variable num_words so that num_words should end up with the total number of words with more than 3 characters.
words = ["water", "chair", "pen", "basket", "hi", "car"]
num_words = 0
for i in words:
if len(i) > 3:
num_words += 1
@mayankdawar
mayankdawar / chainedCondition_2.py
Created February 19, 2020 17:31
Create an empty list called resps. Using the list percent_rain, for each percent, if it is above 90, add the string ‘Bring an umbrella.’ to resps, otherwise if it is above 80, add the string ‘Good for the flowers?’ to resps, otherwise if it is above 50, add the string ‘Watch out for clouds!’ to resps, otherwise, add the string ‘Nice day!’ to res…
percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]
resps = []
for i in percent_rain:
if i > 90:
resps.append("Bring an umbrella.")
elif i >80:
resps.append("Good for the flowers?")
elif i > 50:
resps.append("Watch out for clouds!")
else:
@mayankdawar
mayankdawar / chainedCondition_1.py
Created February 19, 2020 17:30
Create one conditional to find whether “false” is in string str1. If so, assign variable output the string “False. You aren’t you?”. Check to see if “true” is in string str1 and if it is then assign “True! You are you!” to the variable output. If neither are in str1, assign “Neither true nor false!” to output.
str1 = "Today you are you! That is truer than true! There is no one alive who is you-er than you!"
if 'false' in str1:
output = "False. You aren't you?"
elif "true" in str1:
output = "True! You are you!"
else:
output = "Neither true nor false"