Skip to content

Instantly share code, notes, and snippets.

@cibofdevs
Created July 16, 2019 18:06
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save cibofdevs/7d679cf970af181a42a25fb1c1444f10 to your computer and use it in GitHub Desktop.
Save cibofdevs/7d679cf970af181a42a25fb1c1444f10 to your computer and use it in GitHub Desktop.
# 1. 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"
scores_split = scores.split(" ")
a_scores = 0
for x in scores_split:
x = float(x)
if x >= 90:
a_scores += 1
print(a_scores)
# 2. 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 the list stopwords.
# For example, if org was assigned the string “hello to world” then the resulting acronym should be “HW”.
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"
stopwords = set(w.upper() for w in stopwords)
acro = ''.join(i[0] for i in org.upper().split(' ') if i not in stopwords)
# 3. 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 stored in the list stopwords.
# For example, if sent was assigned the string “height and ewok wonder” then the resulting acronym should be “HE. EW. WO”.
stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', 'The']
sent = "The water earth and air are vital"
acro = '. '.join(word[:2].upper() for word in sent.split() if word not in stopwords)
print(acro)
# 4. 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]
# 5. 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 for 29.99 USD.
inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25, 15.00", "scarves, 13, 7.75"]
for item in inventory:
item_desc, number, cost = item.split(", ")
print("The store has {} {}, each for {} USD.".format(number, item_desc, cost))
@SABRIOUS
Copy link

SABRIOUS commented Dec 10, 2019

`stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"
a = org.split(" ")

for i in stopwords:
    if i in a:
        a.remove(i)

final_list = []

for i in a:
    if i[-1] !=",":
        final_list.append(i)
    else:
        final_list.append(i[0:-1])

new = ""
for i in final_list:
    new += i[0]

acro= new.upper()
`

@donxavier20
Copy link

donxavier20 commented Apr 26, 2020

2. Q

   stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
   org = "The organization for health, safety, and education"

   acro=""

   orglist=org.split()

   for firstw in orglist:
  
            if firstw not in stopwords:
        
                   acro=acro+firstw[0].upper()

@shawonAlam
Copy link

can you explain line no 41(r_phrase = p_phrase[::-1])?

@bibeknp
Copy link

bibeknp commented May 27, 2020

stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', 'The']
sent = "The water earth and air are vital"

acro = ""
sent_words = sent.split()
for word in sent_words:
    if word not in stopwords:
        acro = acro + word[:2]
        if word != sent_words[-1]:
            
            acro += ". "
acro = acro.upper()
print(acro)

@Tafhimbn
Copy link

stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"

acro = ""
words = org.split()
for ch in words:
if ch not in stopwords:
acro = acro + ch[:1]

acro = acro.upper()
print(acro)

@smitabhosale15
Copy link

Write a program that extracts the last three items in the list sports and assigns it to the variable last. Make sure to write your code so that it works no matter how many items are in the list.
sports = ['cricket', 'football', 'volleyball', 'baseball', 'softball', 'track and field', 'curling', 'ping pong', 'hockey']

@anabelle2468
Copy link

inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25, 15.00", "scarves, 13, 7.75"]
for item in inventory:
s=item.split(", ")
name = s[0]
stock = s[1]
price=s[2]

print("The store has {} {}, each for {} USD.".format(stock,name,price))

@anabelle2468
Copy link

stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', 'The']
sent = "The water earth and air are vital"
acro=""
o=[]
o=sent.split(" ")
for n in o:
if(n not in stopwords and n!=o[-1]):
a=str(n[0])
b=str(n[1])
acro+=a.upper()+b.upper()+'. '
elif(n==o[-1]):
a=str(n[0])
b=str(n[1])
acro+=a.upper()+b.upper()

@Naveenka9538
Copy link

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"

mlist = []
a_scores = 0
for score in scores.split():
if int(score)>=90:
a_scores += 1
mlist.append(score)

stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"

acro = ''
for string in org.split():
if string in stopwords:
pass
else:
acro += string.capitalize()[0]

stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', 'The']
sent = "The water earth and air are vital"
acro = ". ".join([i[:2].upper() for i in sent.split() if i not in stopwords])

p_phrase = "was it a car or a cat I saw"
r_phrase == p_phrase[::-1]

inventory = ["shoes, 12, 29.99", "shirts, 20, 9.99", "sweatpants, 25, 15.00", "scarves, 13, 7.75"]
for i in inventory:
print("The store has {} {}, each for {} USD.".format(i.split(', ')[1],i.split(', ')[0],i.split(', ')[2]))

@hadrocodium
Copy link

"""
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 the list stopwords.

For example, if org was assigned the string “hello to world” then the resulting acronym should be “HW”.
"""


stopwords = [
          'to',
          'a',
          'for',
          'by',
          'an',
          'am',
          'the',
          'so',
          'it',
          'and',
          "The"
]


org = "The organization for health, safety, and education"

words = org.split()
acro = ''

for word in words:
    if word in stopwords:
        continue
    acro += word[0].upper()

print(acro)

@AdrianMXO
Copy link

#what I am doing wrong?

stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"
acro = []
s_sorg = org.split(" ")

for i in stopwords:
if i in s_sorg:
s_sorg.remove(i)

for a in s_sorg:
acro = a
print (acro[0].upper())

@Elijah57
Copy link

1. 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"
scores_list = scores.split(" ")

scores_above_90 = [I for I in scores_list if I > 90]
a_scores = len(scores_above_90)

@Elijah57
Copy link

2. 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 the list stopwords.

For example, if org was assigned the string “hello to world” then the resulting acronym should be “HW”.

stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
org = "The organization for health, safety, and education"

def make_acronymn(words):

stopwords = ['to', 'a', 'for', 'by', 'an', 'am', 'the', 'so', 'it', 'and', "The"]
stopwords = set(word.upper() for word  in stopwords)

words_ = words.upper().split(" ")

Acronymn = [i[0] for i in words_ if i not in stopwords]
Acronymn = "".join(Acronymn)

print(Acronymn)

make_acronymn(org)

@henryd0
Copy link

henryd0 commented Oct 4, 2022

r_phrase = p_phrase[::-1]
can someone explain this part " :: " ?
Thank you!

@Ivan6240
Copy link

Ivan6240 commented May 1, 2023

The part #p_phrase[::-1] is a Python slice notation that reverses the order of the characters in the string variable p_phrase. It starts with the colon ":" which means the slice starts at the beginning of the string, and since no end index is specified, it goes to the end of the string. The "-1" after the colon indicates that the step size is -1, which means the slice moves through the string backward, one character at a time. The result is a new string variable r_phrase that contains the reversed version of p_phrase

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment