r/learnpython • u/Next-Flounder-8998 • 1d ago
need help with this lab
Write a program that will combine two integer arrays into one larger array and remove any duplicate values.
Prompt the user for a number N, (N will be a positive integer greater than 1), then create two arrays each with N elements. Next, populate both arrays with random numbers between 0 and
500. Finally combine the two arrays into a third array making sure that only distinct values are added. I.e. if the number '65' exists in both arrays, only one instance of it will be added to the third array.
Your program should output contents of the third array, one number per line.
Before you hand it in, make sure to test your program with an A value of:
• 10
25
50
Input:
N: 25
Output:
The contents of the final array, one number per line.
7
u/ninhaomah 1d ago
So... What have you done so far ?
3
u/zanfar 1d ago
OK, what do you need help with? We're not doing your homework for you.
1
u/duskharborings 1d ago
what part are you stuck on specifically? usually helps to break it down into steps, like getting the user input working first, then using the random module to build your two lists before worrying about duplicates.
1
u/peakpirate007 1d ago
been building https://www.runtime.school/ for learning and practicing python in one place. lessons, practice, projects and a few tools all use the same datasets, so it feels more connected than jumping between random examples. if you’re learning python , might be useful.
1
u/Available_Safe1818 1d ago
It's surprising that you seem to not understand that everyone else is not typing in bold and that you should also not type everything in bold. I would expect that from a 12 year old.
1
u/lakseol 9h ago
I'm guessing, because your formatting lost all indentation and added extra stuff, but your code probably looks like this:
I added two debug prints and made the random range smaller, 0 to 10. Those changes make it easier to check that the code is working properly. Here is a small run to show the results:
It's now easy to see that your code is working correctly. You should get into the habit of adding prints to check the operation of your code. Remove the debug prints before submitting the code.
1
u/Atmosck 1d ago
Do you know if the intention is for you to write these operations yourself in pure python with loops and stuff? Or to use third party (but common) libraries like numpy? I'm guessing the latter because you used the word "array" which isn't really a thing in pure python, and numpy is very well suited to this sort of stuff.
1
u/a-walking-bowl 1d ago
No one's doing your homework for you. Learning is different from spoonfeeding. Go write your own code, break it fifty times and understand what is going on instead of getting it written
0
u/EnvironmentalDot9131 1d ago
that's it it is done fkr
0
u/Next-Flounder-8998 1d ago
import random
21
22
23
24
25
N = int(input("N: "))
while N < 1:
print('N must be a positive integer greater than 1.")
N = int (input("N: "))
26
27
28
array1 = []
29
for i in range(N):
30・
31
! | array1. append (random. randint (0,500))
32
33
34
35
array2 = |
for i in range(N):
| array2.append(random.randint (0,500))
36
37
38
39
40
41
array3 =.[]
for num in array1:
if num not in array3: array3.append (num)
42
43
44
45
46
for num in array2:
if num not in array3: | array3.append (num)
47
48
for num in array3: print (num)