r/learnpython • u/Next-Flounder-8998 • 21h 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.
1
1
u/duskharborings 20h 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 16h 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 10h 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/Atmosck 21h 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 21h 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/Next-Flounder-8998 20h 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)
1
1
u/xenomachina xenomachina 16h ago
You code isn't formatted properly, so it's pretty hard for anyone here to read it.
Paste it in the box
Select it
Click the "code" button (looks like
</>).Don't do whatever you're doing that's making all of your comments bold and inserting numbers. (I'd be more specific, but I've never seen anyone have this problem.)
7
u/ninhaomah 21h ago
So... What have you done so far ?