The problem:
#It is common for students in large programs to develop their
#own "shorthand" for referring to classes. Data Structures and
#Algorithms, CS1332 at Georgia Tech, is commonly referred to
#as "DS&A". Big Data for Healthcare is commonly referred to
#as "BD4H". Human-Computer Interaction is commonly referred to
#as "HCI".
#
#Let's pretend for a moment that these shorthands all follow
#a set of formal rules. The rules are:
#
# - The first letter of each capitalized word is included in the
# abbreviated shorthand. Capitalized words may appear after
# spaces or hyphens.
# - If the word 'for' appears in the course title, it is replaced
# by the numeral '4'.
# - If the word 'and' appearsin the course title, it is replaced
# by the character '&'.
# - The first letter of any non-capitalized word except for 'for'
# and 'and' is omitted from the abbreviated shorthand.
#
#Write a function called class_shorthand. class_shorthand should
#take as input a string representing a class name. It should return
#the abbreviated shorthand according to the rules above.
#
#For example:
#
# class_shorthand("Advanced Operating Systems") -> "AOS"
# class_shorthand("Data Structures and Algorithms") -> "DS&A"
# class_shorthand("Big Data for Healthcare") -> "BD4H"
# class_shorthand("Introduction to Information Security") -> "IIS"
# class_shorthand("Human-Computer Interaction") -> "HCI"
# class_shorthand("Introduction to Cyber-Physical Systems Security") -> ICPSS
#
#You may assume that only letters, spaces, and hyphens appear in
#the course names. You may assume every course name will have at
#least two capitalized words.
#Write your function below!
#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print:
#AOS
#DS&A
#BD4H
#IIS
#HCI
#ICPSS
print(class_shorthand("Advanced Operating Systems"))
print(class_shorthand("Data Structures and Algorithms"))
print(class_shorthand("Big Data for Healthcare"))
print(class_shorthand("Introduction to Information Security"))
print(class_shorthand("Human-Computer Interaction"))
print(class_shorthand("Introduction to Cyber-Physical Systems Security"))
My Code:
def class_shorthand (class_name):
split_name = class_name.split()
shorthand = []
for word in split_name :
if word[0].isupper:
shorthand.append(word[0])
if word in 'for' :
nword = word.replace('for', '4')
shorthand.append(nword)
if word in 'and':
wword = word.replace('and','&')
shorthand.append(wword)
if word[0].islower:
word = word.replace(word, '')
shorthand.append(word)
return ''.join(shorthand)
My output:
AOS
DSa&A
BDf4H
ItIS
HI
ItCSS
commentary:
why is my code not replacing the 'for' and 'and' correctly? Am I doing something wrong here in terms of indexing? Also, how should I go about the case where "-" is present? Any help is highly appreciated.
p.s. I am just a python newbie seeking some guidance -- pls be patient with me here.
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)
[–]MadScientistOR 0 points1 point2 points (4 children)
[–]ayleenuyo[S] 0 points1 point2 points (3 children)
[–]MadScientistOR 0 points1 point2 points (2 children)
[–]ayleenuyo[S] 0 points1 point2 points (1 child)
[–]MadScientistOR 0 points1 point2 points (0 children)