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...
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 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 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 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...
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
In arduino: 1.Given two Arrays A= {2,4,7,8,3) and B={11,3,2,8,13). Write a program that find the numbers...
In arduino: 1.Given two Arrays A= {2,4,7,8,3) and B={11,3,2,8,13). Write a program that find the numbers into A but not in B. For the example C=A-B= {4,7} Print the values (Use for loops) 2.Create a vector of five integers each in the range from -10 to 100 (Prompt the user for the five integer x). Perform each of the following using loops: a) Find the maximum and minimum value b)Find the number of negatives numbers Print all results
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers,...
In the class MyArray, write a method named indexAndCountOfMax that on an input array of numbers, finds and returns (1) the smallest index of the largest element of the array and (2) the number of times the largest element occurs in the array. The header of the method should be public static int[ ] indexAndCountOfMax (double[ ] A). The method should return an array of length 2, where the value at index 0 is the smallest index of the largest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT