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 / StringFormatting.py
Created February 22, 2020 21:37
Provided is a list of data about a store’s inventory where each item in the list represents the name of an item, how much is in stock, and how much it costs. Print out each item in the list with the same formatting, using the .format method (not string concatenation). For example, the first print statment should read The store has 12 shoes, each…
inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25, 15.00", "scarves, 13, 7.75"]
for temp in inventory:
temp = temp.split(',')
str1="The store has{} {}, each for{} USD."
str1=str1.format(temp[1],temp[0],temp[2])
print(str1)
@mayankdawar
mayankdawar / reversedPalindrome.py
Created February 22, 2020 20:50
A palindrome is a phrase that, if reversed, would read the exact same. Write code that checks if p_phrase is a palindrome by reversing it and then checking if the reversed version is equal to the original. Assign the reversed version of p_phrase to the variable r_phrase so that we can check your work.
p_phrase = "was it a car or a cat I saw"
r_phrase = p_phrase[::-1]
@mayankdawar
mayankdawar / ConditionalAcronym.py
Created February 22, 2020 20:34
Write code that uses the string stored in sent and creates an acronym which is assigned to the variable acro. The first two letters of each word should be used, each letter in the acronym should be a capital letter, and each element of the acronym should be separated by a “. ” (dot and space). Words that should not be included in the acronym are…
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', 'The']
sent = "The water earth and air are vital"
acro = ""
lst = sent.split()
for i in lst:
if i in stopwords:
lst.remove(i)
for j in lst:
@mayankdawar
mayankdawar / acronym.py
Created February 22, 2020 20:23
Write code that uses the string stored in org and creates an acronym which is assigned to the variable acro. Only the first letter of each word should be used, each letter in the acronym should be a capital letter, and there should be nothing to separate the letters of the acronym. Words that should not be included in the acronym are stored in t…
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"
acro = ""
lst = org.split()
for i in lst:
if i in stopwords:
lst.remove(i)
for j in lst:
@mayankdawar
mayankdawar / SplitScores.py
Created February 22, 2020 20:06
Below are a set of scores that students have received in the past semester. Write code to determine how many are 90 or above and assign that result to the value a_scores.
scores = "67 80 90 78 93 20 79 89 96 97 92 88 79 68 58 90 98 100 79 74 83 88 80 86 85 70 90 100"
lst = scores.split()
a_scores = 0
for i in lst:
if int(i) >= 90:
a_scores += 1
@mayankdawar
mayankdawar / ForAppend_3.py
Created February 22, 2020 19:57
For each string in wrds, add ‘ed’ to the end of the word (to make the word past tense). Save these past tense words to a list called past_wrds.
wrds = ["end", 'work', "play", "start", "walk", "look", "open", "rain", "learn", "clean"]
lst = []
for i in wrds:
i = i + "ed"
lst.append(i)
past_wrds = lst
@mayankdawar
mayankdawar / ForAppend_2.py
Created February 22, 2020 19:53
For each character in the string saved in ael, append that character to a list that should be saved in a variable app.
ael = "python!"
lst = []
for i in ael:
lst.append(i)
app = lst
@mayankdawar
mayankdawar / ForAppend.py
Last active September 21, 2022 19:52
Currently there is a string called str1. Write code to create a list called chars which should contain the characters from str1. Each character in str1 should be its own element in the list chars.
str1 = "I love python"
chars = []
for i in str1:
chars.append(i)
@mayankdawar
mayankdawar / OppositeSort.py
Created February 22, 2020 19:29
Write code to switch the order of the winners list so that it is now Z to A. Assign this list to the variable z_winners.
winners = ['Alice Munro', 'Alvin E. Roth', 'Kazuo Ishiguro', 'Malala Yousafzai', 'Rainer Weiss', 'Youyou Tu']
winners.sort(reverse=True)
z_winners = winners
@mayankdawar
mayankdawar / appendFunc.py
Created February 22, 2020 19:21
Write code to add ‘Guadalajara’ to the end of the list trav_dest using a list method.
trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'Melbourne']
trav_dest.append("Guadalajara")