Question

In: Computer Science

Write a program named “hw2-extra.py” that will find duplicate numbers in an array.Here is a sample...

Write a program named “hw2-extra.py” that will find duplicate numbers in an array.Here is a

sample run of the program. An example for an input is as follows. The program tells you “The

7

duplicate numbers: 2 3 7 1”.

Again, do not use built-in functions if there is any.

You can read

the input from the file named as “hw2-ExtraCredit.txt”

235231779 13 15 99 96 4 1

The duplicate numbers: 2 3 1 7

Solutions

Expert Solution

Answer : please up vote or comment for any query i will update the same . Thanks

Check output image and program image for indentation

Note :- it will treat first numbers as a single digit separate number as u treated in output

Program plan :

1. open file read one by one

2. treat numbers between two spaces as a single number

3. check in list for matching

4. if found also check in duplicate number to append or not append

5. display duplicate number

Program :

List=[] #store one by one character
Duplicate=[] #store duplicate number
space=0 #to store numbers between two space a single number
num="" #append numbers between two space

def Find(a,List,x): #find a number between list x= index number of list to find
i=x+1 #start check +1 of current index to count current index as match
while i<len(List):
if(int(a)==int(List[i])): #if found return 1
return 1
i+=1
return -1 #else return -1
  

with open("hw2-ExtraCredit.txt") as fileobj: #open file
for line in fileobj:
for ch in line: #read chracter by character in line
if(ch!=' ' and space==0):

List.append(ch) #only add numbers not space

elif(ch==' ' and space==0): #if space append numbers between space
space=1
elif(ch!=' ' and space==1):
num+=ch
elif(ch==' ' and space==1):
List.append(num)
num=""
space=1
if(num!="" and num!=' '): #last element of file append if a number
List.append(num)

  

i=0   
while i<len(List): #print file data
print("File Data",int(List[i]))
i+=1

i=0

while i<len(List): #run a loop 0 to last element
if(Find(List[i],List,i)==1): #check for match in List
if(Find(List[i],Duplicate,-1)!=1): #also check in duplicate if already appended
Duplicate.append(List[i])
i+=1
  

print("the Duplicate number is: ",Duplicate) #print list


Output Images :

Output


Related Solutions

Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers and the recursive formula please refer to problem 5 of Chapter 2 at page 81 in the text-book.) The program asks the user to input an integer and outputs the corresponding Fibonacci number. For example, if the user inputs 4, the program outputs the fifth Fibonacci number, which is 3. Introduce two local variables in the recursion function (suppose we call it fib(n)), one...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Write a JavaScript program with a function named fives that reads two numbers from two text...
Write a JavaScript program with a function named fives that reads two numbers from two text fields and then outputs to a div "True" if both of the numbers are greater than 5 or if their sum is greater than 20. Otherwise your function should output "False" to the div. If you wish, you may use the following HTML code to begin your program.
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
write a program to find the maximum possible sum such that no two chosen numbers are...
write a program to find the maximum possible sum such that no two chosen numbers are adjacent either vertically, horizontally, or diagonally. code in java
Write a program to produce an array of integer random numbers. Your program should find out...
Write a program to produce an array of integer random numbers. Your program should find out from the user how many numbers to store. It should then generate and store that many random integers (the random numbers must be between 1 and 999 inclusive). The program should then determine the smallest number, the largest number, and the average of all the numbers stored in the array. Finally, it should print out all the numbers on the screen, five numbers to...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT