r/learnpython • • 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.

2 Upvotes

19 comments sorted by

7

u/ninhaomah 1d ago

So... What have you done so far ?

3

u/a-walking-bowl 1d ago

he posted on reddit and asked strangers for help... but no one was willing. how heartbreaking for him /s

-3

u/Next-Flounder-8998 1d ago

i have it done but my professor was saying that my third array removed both duplicates instead of only one of them or something along those lines i can’t remember exactly what he said but it was something with the outcome of my third array and duplicates.

5

u/fakemoose 1d ago

So share your code

Also why is all your text in bold?

3

u/Binary101010 1d ago

We can't tell you why that happened if you don't show us your code.

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:

import random

N = int(input("N: "))
while N < 1:
    print("N must be a positive integer greater than 1.")
    N = int(input("N: "))

array1 = []
for i in range(N):
    array1.append(random.randint(0, 10))#500))  # debug, smaller range is easier to check

print(f"{array1=}")  # debug

array2 = []
for i in range(N):
    array2.append(random.randint(0, 10))#500))  # debug, smaller range is easier to check
    
print(f"{array2=}")  # debug

array3 = []
for num in array1:
    if num not in array3:
        array3.append(num)

for num in array2:
    if num not in array3:
        array3.append(num)

for num in array3:
    print(num)

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:

N: 5
array1=[1, 5, 10, 2, 3]
array2=[1, 1, 9, 7, 10]  # check these arrays against the result below
1
5
10
2
3
9
7

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)

1

u/Penguinase 1d ago

what are you using to edit your code..?

1

u/Next-Flounder-8998 1d ago

vs code and python

1

u/xenomachina xenomachina 1d ago

You code isn't formatted properly, so it's pretty hard for anyone here to read it.

  1. Paste it in the box
  2. Select it
  3. 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.)