#๐ I don know if what i did is correct or bad
122 messages ยท Page 1 of 1 (latest)
@strange wren
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.
I don't have time to do a full review, but the code in the screenshot is not legal Python.
does it pass all of the test?
To check equality, you need ==. = is for assigning a value, and isn't legal where you have it.
ohhh
yes i forgor
but is the logoccorrect?
logic
wat test
try running it
You can't just write code and then guess if it's functional. You need to be running and verifying your code as you write it.
shud i call the array too, for it to run?
i don know how this works
what happens if you run it as-is?
as-is?
it says
can you show the full instructions on the left?
what is this? are you doing something like codewars or leetcode? or are you doing some online course study?
Freecodecamp
ah ok
can I get the link to that lesson? I'll try it myself. They're probably doing it differently
okok
My guess is that you have to execute each test explicitly in the code
shud i call the array for it to be executed?
i mean call the function
did i just define it only?
Do you see a console to the right?
yes
does it show anything when you run your code?
above this, there is a black background console. What does it show?
Your code is constantly being tested to see if its Python valid and that shows in the console on the right
No๐ญ
so if you had the = in that if condition, it should show that error message in the console
it doesnt
can you take a full screenshot of the whole thing please?
instructions, code, console and the test result
all in one
Strange but ok and now click on the "Run the Tests" button and show me a new screenshot after clicking it
hmm
copy your code, paste it here, then click "Reset this lesson" button, then paste your code back in, and click "Run the Tests" button.
The terminal on the right should have caught that. Which is strange.
wai lemme copy
freecodecamp's IDE autoruns the script to check if its valid Python code. So I don't know whats wrong with OP's
def most_frequent(arr):
timesarray = []
for elements in arr:
totaltimes = 0
flag = False
diffrence = len(arr) - elements
for existence in range(diffrence):
if arr[elements] == arr[existence]:
flag = True
if flag == False:
for elements2 in arr:
if arr[element] == arr[elements2]:
totaltimes = totaltimes + 1
timesarray[element] = totaltimes
highestindex = 0
for elements3 in timesarray:
if timesarray[elements3] > timesarray[highestindex]:
highestindex = elements3
return arr[highestindex]
return arr
Hey @strange wren!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
no its still the same
show me a full screenshot
thats not right...
yes its not
it doesnt show
which web browser are you using?
you did click on "Reset this lesson" button, before pasting your code back in, correct?
yes
I think something is blocking your script running in freecodecamp. Can you open up devtools?
Press F12
And show me the Console tab
im on ipad๐
๐คฆโโ๏ธ
lmao
ill js download the app ahh๐
ok so now that button makes sense. When you press that "Run the Tests" button, it should revert back to black button. The button looks like its still being pressed.
yes
'k, now let's get to debugging the actual code
๐
woww i don hab that
line 6 will throw an error in test case 1
it's doing len(arr) - elements but elements will take on the value of letters
!e 4 - "a"
:x: Your 3.14 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File [35m"/home/main.py"[0m, line [35m1[0m, in [35m<module>[0m
003 | [31m4 [0m[1;31m-[0m[31m "a"[0m
004 | [31m~~[0m[1;31m^[0m[31m~~~~[0m
005 | [1;35mTypeError[0m: [35munsupported operand type(s) for -: 'int' and 'str'[0m
the first thing it will do is throw an error
btw its my first time in python, im switching languages ๐ญ
no worries
you can chain functions together to make the code a bit more readable
for index in range(len(arr)):
ohhh okok
How's your progress?
I'm getting confused about most of the code on lines 7 through 14
I know you're counting how many times it appears, but the how eludes me
im checking if the data that i currently have in arr[elements] is already existing in the array until its index
if ot exists then i dont count it
if it doesnt exist then i count how many times ot exists in the array and i store it
if the array is [a,b,a,d,a] then when im looping i dont want to count for the 3rd element in the array bcz a is the first element already and i counted for it already
ok, say I feed your function the array: [1,1,1,2]
it will create a timesarray that looks like: [1,2,3,1]?
i want the times array to look like[3, , , 1]
after this, you should learn through the basics on Python and maybe start at day 1 of the daily coding challenge. Its a lot simpler and easier to tackle than the later days.
but try and finish this one first though
Okokayy
So, the simple way of solving this would be to just count the items in the array repeatedly.
That way, an input of: [1,1,1,2]
Becomes timesarray = [3,3,3,1]
Your solution is maybe more optimized, but still has O(nยฒ) complexity
loll okayyy
ill do that
thanksss
Done. Did it without collections or counter. Feels dirty using 2 loops
pseudocode wise, I used a dictionary to keep count of each unique element in the array and then loop the dictionary to find the highest count. I'm pretty sure there are better ways to do it
I'm tempted to do it in one line
This help channel has been closed. 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.