importmath#Functionsdeffind_volume_sphere(radius):"""Calculates the volume of a sphere with a given radius"""return(4/3)*math.pi*radius**3deffind_volume_cube(side):"""Calculates the volume of a cube with a given side length"""returnside**3#Inputradius=str(input("Enter the gum ball radius (in.) => ").strip())print(radius)weekly_sales=str(input("Enter the weekly sales => ").strip())print(weekly_sales,"\n")#Calculationstarget_sales=math.ceil(float(weekly_sales)*1.25)edge_gumballs=math.ceil(target_sales**(1/3))edge_length=edge_gumballs*float(radius)*2edge_gumballs_max=edge_length/(float(radius)*2+0.0000000000000001)#Aviod ZeroDivisionError by adding a small number to the radiusnumber_extra_gumballs=math.ceil(edge_gumballs_max**3-target_sales)volume_gumballs=find_volume_sphere(float(radius))volume_cube=find_volume_cube(edge_length)volume_wasted_target=volume_cube-volume_gumballs*target_salesvolume_wasted_full=volume_cube-volume_gumballs*(edge_gumballs_max)**3#Printprint("The machine needs to hold",str(edge_gumballs),"gum balls along each edge.")print("Total edge length is","{:.2f}".format(edge_length),"inches.")print("Target sales were",str(target_sales)+", but the machine will hold",str(int(number_extra_gumballs)),"extra gum balls.")print("Wasted space is","{:.2f}".format(volume_wasted_target),"cubic inches with the target number of gum balls,")print("or","{:.2f}".format(volume_wasted_full),"cubic inches if you fill up the machine.")
user_input=input("Enter a string to encode ==> ").strip()print(user_input,"\n")#Replacingdefencrypt(word):word=word.replace(" a","%4%")word=word.replace("he","7!")word=word.replace("e","9(*9(")word=word.replace("y","*%$")word=word.replace("u","@@@")word=word.replace("an","-?")word=word.replace("th","!@+3")word=word.replace("o","7654")word=word.replace("9","2")word=word.replace("ck","%4")returnword#Calculationlength_difference=abs(len(user_input)-len(encrypt(user_input)))#Decodingdefdecrypt(word):word=word.replace("%4","ck")word=word.replace("2","9")word=word.replace("7654","o")word=word.replace("!@+3","th")word=word.replace("-?","an")word=word.replace("@@@","u")word=word.replace("*%$","y")word=word.replace("9(*9(","e")word=word.replace("7!","he")word=word.replace("%4%"," a")returnword#Printingprint("Encrypted as ==>",encrypt(user_input))print("Difference in length ==>",str(length_difference))print("Deciphered as ==>",decrypt(encrypt(user_input)))ifuser_input==decrypt(encrypt(user_input)):print("Operation is reversible on the string.")else:print("Operation is not reversible on the string.")
defnumber_happy(sentence):happy_words=["laugh","happiness","love","excellent","good","smile"]sentence=sentence.lower()#sentence = sentence.strip()#sentence = sentence.split()count=0forwordinhappy_words:count+=sentence.count(word)returncountdefnumber_sad(sentence):sad_words=["bad","sad","terrible","horrible","problem","hate"]sentence=sentence.lower()#sentence = sentence.strip()#sentence = sentence.split()count=0forwordinsad_words:count+=sentence.count(word)returncount#Get user input#sentence = "I laughed and laughed at her excellent joke."sentence=input("Enter a sentence => ").strip()#Print#print(number_happy(sentence))print(sentence)print("Sentiment: "+("+"*number_happy(sentence))+("-"*number_sad(sentence)))ifnumber_happy(sentence)>number_sad(sentence):print("This is a happy sentence.")elifnumber_happy(sentence)==number_sad(sentence):print("This is a neutral sentence.")else:print("This is a sad sentence.")