Page 1 of 1

**Having difficulty figuring out the printIntegerSequenceInformation function. Included is the printMenu and the isvalid

Posted: Mon May 09, 2022 6:17 am
by answerhappygod
**Having difficulty figuring out
the printIntegerSequenceInformation
function. Included is the printMenu and the
isvalidindex that was given.
Given the Python program provided in the template file, you must
write the definition and implementation of the following
user-defined functions:
Each line has multiple integer values (positives and negatives,
separated by a single white space) representing a sequence of
numbers. The function creates a list of lists where each element of
the list is a list that stores the information of an integer
sequence (you must store each value in the sublists as integers).
Finally, the function returns the list with all the integer
sequences. Based on the previous input file, the list returned by
the function is:
Having Difficulty Figuring Out The Printintegersequenceinformation Function Included Is The Printmenu And The Isvalid 1
Having Difficulty Figuring Out The Printintegersequenceinformation Function Included Is The Printmenu And The Isvalid 1 (35.83 KiB) Viewed 32 times
Having Difficulty Figuring Out The Printintegersequenceinformation Function Included Is The Printmenu And The Isvalid 2
Having Difficulty Figuring Out The Printintegersequenceinformation Function Included Is The Printmenu And The Isvalid 2 (46.66 KiB) Viewed 32 times
Having Difficulty Figuring Out The Printintegersequenceinformation Function Included Is The Printmenu And The Isvalid 3
Having Difficulty Figuring Out The Printintegersequenceinformation Function Included Is The Printmenu And The Isvalid 3 (31.87 KiB) Viewed 32 times
Having Difficulty Figuring Out The Printintegersequenceinformation Function Included Is The Printmenu And The Isvalid 4
Having Difficulty Figuring Out The Printintegersequenceinformation Function Included Is The Printmenu And The Isvalid 4 (115.68 KiB) Viewed 32 times
def isValidIndex( index, lst): if (index - 1) >= 0 and (index - 1) < len(lst): return True return false def printMenu(): print() print("1. Print Integer Sequences") print("2. Print an Integer Sequence") print("3. Add a value to an Integer Sequence") print("4. Delete a value from an Integer Sequence") print("5. Modify a value from an Integer Sequence") print("6. Delete an Integer Sequence") print("7. Exit") print()
= 1 v def readFromFile(filename): 2 fh = open(filename) 3 Listof Sequences = [] 4 for line in fh: 5 if line[-1] == '\n': 6 line = line[:-1] 7 sequence = line.split(" ") 8 for i in rangel len( sequence)): 9 sequence = int( sequence) 10 ListOfSequences.append( sequence) 11 fh.close() 12 return ListOfSequences 13 14 v def printIntegerSequence(ListOfSequences): 15 for no, sequence in enumerate(ListOfSequences, start=1): 16 print(f"Sequence #{no}") 17 sequence list(map( str, sequence)) 18 print(" ".join( sequence))
def print Integer SequenceInformation(ListOfSequences): P_count=0 n_count=0 i=0 print("values:", end=" ") 1 = ListOfSequences while i<len(l): print(l, end=" ") if(l>0); p_count==1 elif(l<0): n_count+=1 i+=1 print("\n# of values:", len(ListOfSequences )) print("# Positive values:",p_count) print("# Negative values:",n_count)
112 filename = input("Enter the file name: ") 113 integer Sequences = readFromFile(filename) 114 menuOption = 0 115 while menuOption != 7: 116 printMenu() 117 menuOption = int(input("Enter your option: ")) 118 if menuOption == 1: 119 print Integer Sequences( integerSequences) 120 elif menuOption >= 2 and menuOption <= 6: # Reading the sequence number for options 2,3,4,5,6. Only a value between 1 and the number of sequences is a valid sequence number 121 sequenceNo = int(input("Enter the sequence number: ")) 122 if isValidIndex( sequenceNo, integer Sequences) == False: 123 print("Invalid sequence number") 124 else: 125 if menuOption == 2: # Printing the information of the selected sequence 126 print Integer Sequence Information integer Sequences[sequenceNo-1]) 127 elif menuOption != 6: # Reading the position for options 3,4,5. 128 pos = int(input("Enter the position: ")) 129 if menuOption == 3: # When adding a value to a sequence, -1 is a valid position (adds the element at the end of the sequence) 130 validPosition = isValidIndex(pos, integer Sequences[sequenceNo-1]) or (pos == -1) 131 else: # For the other functions (remove and modify), only a value between 1 and the number of elements is a valid position 132 validPosition = isValidIndex(pos, integerSequences[sequenceNo-1]) 133 if validPosition == False: 134 print("Invalid position") 135 else: =