#๐ : Given an array of n positive numbers. All numbers occurs even number of times except 1 whichoccu
167 messages ยท Page 1 of 1 (latest)
Hey @shell heart!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
@shell heart
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
do you have a question that you want to ask
Yes I do not understand the dry run of this code
Do you understand what ^= does?
what does 'dry run' mean?
Yes i understand
Explanation of it
What are you unsure of?
Do you understand function definitions? Do you understand loops?
yes i understand all of that, the only issue is that i am unable to understand it is working, i know that even ones are cancelling out, odd one would remain
but how is that carrying out, i do not understand
what do you think ^= does?
that is the confusion
You just said here you understand though?
okay maybe there is the confusion
do you understand what binary and bits are?
yes i understand
What are they?
binary are 0 and 1
yes i understand them
ok
so ^ will flip bits
we're using ^= to flip bits on and off. If the same number's bits are flipped twice, it's like it never happened
this will happen with any number that appears an even number of times
i understand this result = 0
result ^= 1 => result = 0 ^ 1 = 1
result ^= 2 => result = 1 ^ 2 = 3
result ^= 3 => result = 3 ^ 3 = 0
result ^= 2 => result = 0 ^ 2 = 2
result ^= 3 => result = 2 ^ 3 = 1
result ^= 1 => result = 1 ^ 1 = 0
result ^= 3 => result = 0 ^ 3 = 3
but i do not understand how is this saying 3 got odd count
!e
result = 11
print(f'{result:08b}')
:white_check_mark: Your 3.12 eval job has completed with return code 0.
00001011
look at the binary for 11
!e
result = 11
print(f'{result:08b}')
result ^= 11
print(f'{result:08b}')
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 00001011
002 | 00000000
if we ^= (XOR) the bits, they all get turned off
so xor'ing a number twice is basically the same as not xor'ing it at all
if a number gets xor'd an odd number of times, that means the bits will remain on
whatever bits are on at the end of the loop would be the bits of the number that appeared an odd amount
ok but can you explain what u just said with reference to this
result = 0
result ^= 1 => result = 0 ^ 1 = 1
result ^= 2 => result = 1 ^ 2 = 3
result ^= 3 => result = 3 ^ 3 = 0
result ^= 2 => result = 0 ^ 2 = 2
result ^= 3 => result = 2 ^ 3 = 1
result ^= 1 => result = 1 ^ 1 = 0
result ^= 3 => result = 0 ^ 3 = 3
the actual resulting number in base 10 (decimal) isn't necessarily useful to view
it's the bits that are important
result = 0
result ^= 1 # <-- (a)
result ^= 2 # <-- (b)
result ^= 3 # <-- (c)
result ^= 2 # <-- cancels out (b)
result ^= 3 # <-- cancels out (c)
result ^= 1 # <-- cancels out (a)
result ^= 3
!e
def find_odd_occ(arr):
result = 0
for number in arr:
result ^= number
print(f'{number:04b} {result:04b}')
return result
arr = [1, 2, 3, 2, 3, 1, 3]
print(find_odd_occ(arr)) # Output: 3
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 0001 0001
002 | 0010 0011
003 | 0011 0000
004 | 0010 0010
005 | 0011 0001
006 | 0001 0000
007 | 0011 0011
008 | 3
the binary number on the left is the number we're checking, and the binary number on the right is the result (the bits that are currently on/off)
okay gotcha
!e
def find_odd_occ(arr):
result = 0
for number in arr:
result ^= number
print(f'flipping {number} -- {number:04b} {result:04b}')
return result
arr = [1, 2, 3, 2, 3, 1, 3]
print(find_odd_occ(arr)) # Output: 3
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | flipping 1 -- 0001 0001
002 | flipping 2 -- 0010 0011
003 | flipping 3 -- 0011 0000
004 | flipping 2 -- 0010 0010
005 | flipping 3 -- 0011 0001
006 | flipping 1 -- 0001 0000
007 | flipping 3 -- 0011 0011
008 | 3
okay thankyou
Initial Setup: result starts at 0.
First Number (1):
0
โ
1
1
0โ1=1. Now, result is 1.
Second Number (2):
1
โ
2
3
1โ2=3. Now, result is 3.
Third Number (3):
3
โ
3
0
3โ3=0. The two 3s cancel each other out, result goes back to 0.
Fourth Number (2):
0
โ
2
2
0โ2=2. Now, result is 2.
Fifth Number (3):
2
โ
3
1
2โ3=1. Now, result is 1.
Sixth Number (1):
1
โ
1
0
1โ1=0. The two 1s cancel each other out, result goes back to 0.
Seventh Number (3):
0
โ
3
3
0โ3=3. This is the third occurrence of 3, and result becomes 3.
i was asking chatgpt, it explained me this is the way cancellation is being performed, how is the first occurrence of 3 cancelling out the second occurence of 3 when it only appeared once
only appeared once
No?3appeared 3 times
arr = [1, 2, 3, 2, 3, 1, 3]
# ^ ^ ^
but when the calculation is being done, it appeared just once
the actual int value of the intermediate results don't matter
it's that when you do a ^ x, it'll flip a very specific set of bits in a; these flips are exactly canceled out if you do another ^ x on it
so if you do ^ x an even number of times, it's like doing nothing at all
since only 1 number in the entire array appears odd times, by the end of doing result ^ x for every number x inside of arr, it's like you only did result ^ o where o is the number that appears an odd number of times
Alright
imagine a wall of lightswitches. Each number represents a specific set of lightswitches to interact with
if the switch is on, turn it off, if it's off, turn it on
if you perform the same instruction twice, it's like you did nothing at all
In that case I do not understand this
1 means the lightswitch is on, 0 means it is off
That i understand
I'm saying about this
flipping 1 -- 0001 0001
the binary number on the left is the instructions
the binary number on the right is what switches are currently on/off
0001 0001
so for the first one, we're saying flip the switch on the right
0010 0011
the next one says flip the 3rd switch
so now switch 3 and 4 are on
0011 0000
The next instruction is to flip 3 and 4
now they are both off
0010 0010
then we flip 3
and so on
!e
def find_odd_occ(arr):
result = 0
for number in arr:
result ^= number
print(f'flipping {number} -- {number:04b} {result:04b}')
return result
arr = [1, 2, 3, 2, 3, 1, 3]
arr.sort()
print(find_odd_occ(arr)) # Output: 3
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | flipping 1 -- 0001 0001
002 | flipping 1 -- 0001 0000
003 | flipping 2 -- 0010 0010
004 | flipping 2 -- 0010 0000
005 | flipping 3 -- 0011 0011
006 | flipping 3 -- 0011 0000
007 | flipping 3 -- 0011 0011
008 | 3
maybe it's clearer if we sort the list first
Okay got it
@stone geode sorry m disturbing u again n again but how does that give answer 3 ๐
0011 is the binary for 3
it's the only number that flipped its bits on and never flipped them off
@shell heart
So the right hand side answers be it decimal or binary don't mean anything, they are just switches?
@stone geode
1 sec
ok, I wrote a quick function that will display a number's bits as "on/off" instead of 0 1
!e
instructions = ['off', 'on']
def bin_to_instructions(dig):
for i in f'{dig:04b}':
print(instructions[int(i)], end='|')
print()
bin_to_instructions(2)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
off|off|on|off|
That did not explain my question
I'm putting together an example
but I want you to follow what I'm doing
!e
instructions = ['OFF', 'ON']
def bin_to_instructions(dig):
results = []
for i in f'{dig:04b}':
results.append(instructions[int(i)])
return '|'.join(results)
def find_odd_occ(arr):
result = 0
for number in arr:
#result ^= number
print(number, bin_to_instructions(number))
return result
arr = [1, 2, 3, 2, 3, 1, 3]
#arr.sort()
find_odd_occ(arr)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1 OFF|OFF|OFF|ON
002 | 2 OFF|OFF|ON|OFF
003 | 3 OFF|OFF|ON|ON
004 | 2 OFF|OFF|ON|OFF
005 | 3 OFF|OFF|ON|ON
006 | 1 OFF|OFF|OFF|ON
007 | 3 OFF|OFF|ON|ON
These are each number's instructions
1 says OFF OFF OFF ON
3 says OFF OFF ON ON
maybe this isn't clear because OFF doesn't actually mean "turn it off"
the way it's being used is "FLIP"
But how it is doing 4 times?
it's checking each bit
we can technically ignore the first two instructions since we're dealing with small numbers
!e
instructions = ['....', 'FLIP']
def bin_to_instructions(dig):
results = []
for i in f'{dig:02b}':
results.append(instructions[int(i)])
return '|'.join(results)
def find_odd_occ(arr):
result = 0
for number in arr:
#result ^= number
print(number, bin_to_instructions(number))
return result
arr = [1, 2, 3, 2, 3, 1, 3]
#arr.sort()
find_odd_occ(arr)
But that's just binary conversion
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1 ....|FLIP
002 | 2 FLIP|....
003 | 3 FLIP|FLIP
004 | 2 FLIP|....
005 | 3 FLIP|FLIP
006 | 1 ....|FLIP
007 | 3 FLIP|FLIP
That's just binay conversion i realised
at the start of the program, both lightswitches are off
each number (1, 2, 3) has instructions associated with it
1 is "ignore the left switch, flip the right switch"
so that means if it's on, turn it off
if it's off, turn it on
does this make sense?
But that is just binary conversion of 1
yes
But that's not the solution of the question
code is a series of steps
it's not just about the solution
I'm not talking about the solution, I'm talking about the process that leads to the solution
ok but can you explain what u just said with reference to this
result = 0
result ^= 1 => result = 0 ^ 1 = 1
result ^= 2 => result = 1 ^ 2 = 3
result ^= 3 => result = 3 ^ 3 = 0
result ^= 2 => result = 0 ^ 2 = 2
result ^= 3 => result = 2 ^ 3 = 1
result ^= 1 => result = 1 ^ 1 = 0
result ^= 3 => result = 0 ^ 3 = 3
No sorry, i was just pasting this and was trying to explain what m stuck at
and I'm explaining it in simpler terms so you understand it
the answers at the right, 1,3,0,2,1,0,3, means just switches and not the answer?
it's the current "state" of the switches
yes
converted from binary back into decimal
but we don't really care about the decimal
that i know
then I'm not sure what you're asking
that they are decimal
whatever the final state of the switches is when the loop finishes is the solution
what im asking is that how r we getting 3 as answer
i got when you told as its switch is the only one which is left on
that also i know, just bear one more time with me. in that case ure telling me that
1= off on
3=on on
0=off off
2=on off
1=off on
0=off off
3=on on
this is the answer but how does that make sense
because performing the same set of instructions twice negates it
go find two lightswitches and try it out
it will help you understand
!e
instructions = ['....', 'FLIP']
def bin_to_instructions(dig):
results = []
for i in f'{dig:02b}':
results.append(instructions[int(i)])
return '|'.join(results)
def find_odd_occ(arr):
result = 0
for number in arr:
result ^= number
print(number, bin_to_instructions(result))
return result
arr = [1, 2, 3, 2, 3, 1, 3]
arr.sort()
find_odd_occ(arr)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1 ....|FLIP
002 | 1 ....|....
003 | 2 FLIP|....
004 | 2 ....|....
005 | 3 FLIP|FLIP
006 | 3 ....|....
007 | 3 FLIP|FLIP
remember what it looked like sorted
notice how every other result shows everything off
we turn on right, we turn it off
we turn on left, we turn it off
we turn on both, we turn them both off
then we turn them both on, and leave them on
since there's an odd amount of instructions
this is the explanation you are giving me, is that really happening in the code
result = 0
result ^= 1 => result = 0 ^ 1 = 1
result ^= 2 => result = 1 ^ 2 = 3
result ^= 3 => result = 3 ^ 3 = 0
result ^= 2 => result = 0 ^ 2 = 2
result ^= 3 => result = 2 ^ 3 = 1
result ^= 1 => result = 1 ^ 1 = 0
result ^= 3 => result = 0 ^ 3 = 3
code is actually doing this
^ acts on the binary of a number
okay
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.