#๐ Problems with leet code
175 messages ยท Page 1 of 1 (latest)
@hybrid umbra
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.
@hybrid umbra which question
hmm
you know return function ?
yes
you should not print them
you should return the answer in leetcode for the answer to be matched
check it
return it as a list
yea, if you want output then you need to print it separately but to pass the test case you need to return the value
coz basically on the other end
the function is run
nums = input()
target = input()
class Solution(object):
def twoSum(self, nums, target):
for idx1 in range(0, len(nums)):
val = nums[idx1]
for idx2 in range(idx1 + 1, len(nums)):
val2 = nums[idx2]
if val + val2 == target:
return [idx1, idx2]
Solution().twoSum(nums, target)
this is my full code
Oh wait
also
You aren't supposed to have those inputs or the last line
the num and range is inputed by leetcode not you
calling Solution at the end wouldn't even call the method
so it's really doing nothing
Leet code does stuff in the background?
in the backend yes
well yeah, cuz it has to test stuff
so basically this is what you neeeded to do
ohh i never knew that, i thought we had to get all the inputs and call the functions
how you get it
That makes al ot of sense thank you
yep ty
so it runs all the xyz test cases, which include all the test cases and everything
edge cases and all
welcome
you are smart btw
๐ ๐ ty so much
coz when i started leetcode i didnt know what a class was and how to exexute you own class funcs and what self was
keep it up, and how advanced are you in programming, like you migrating from other language?
ty :D i only know how to code python
have you come across data structs and algo?
ive done aiosqlite and SQL
as well as json
okay, just telling you thing coz i would have wished for the same advice
learn some basics of what is time complexity and space complexity
in terms of optimising the code?
yea
oh yh ill prob try rewriting this code because it uses nested loops
and use neetcode .io to look for different solutions, for python there are videos
ill look into it ty
there is also rosetta code
when you do message me the code, i wanna see how you solve it as a new dude
oh
often rosstta code has the alternate versions of code that are more effiecent
sometimes i see some crazy code on leetcode like these
this one is for reversing a linked list
funniest one ive seen is sleep sort
this is the least memory taxing sol
but how does one even think of these
and is it even useful learning these
Sure
im trying to find a different way now
yea, there is a really nice way to solve this, one you hear it (i this case read it) you wont be able to forget it
you wanna know or you wanna try for yourself?
@hybrid umbra
im thinking of enumerate
there is a waaaaaaaaaaaaaaaaay better sol
how else would you do it?
list2 = [3,2,1,4,5,6,7,8,9,10]
def sleep_sort(val):
import time
import threading
## create 1 thread for each value
for i in val:
threading.Thread(target=lambda i=i: (time.sleep(i), print(i))).start()
## get the thread to wait for all threads to finish
## get
time.sleep(max(val)+1)
sleep_sort(list2)```
baiscaly you create a thread for each element, then sleep basied on the value of the element, the return will be naturaly sorted ;
yea so you run a for loop across the list's range
then which ever element you are in ->you search for the number that is (target-element) you are in
ohh
but wouldnt that cause
problems
with duplicate numbers
that and it takes time N to work
and yo uwould need to prevent it from checking the same numbre
dosnt matter if your only searching for unqiue elments
so yeah it natrualy gives you unqiue sorted list
but honestly this is such a dumb algorithm for many many reasons
not only the fact its On time but all On threads
explaain please
im new to this too
O notation is used to denote complexity in both time and calualtion
n being the number of elments in the list
Big O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. Big O is a member of a family of notations invented by German mathematicians Paul Bachmann, Edmund Landau, and others, collectively called BachmannโLandau notation or asymptotic notation. T...
the advantage i guess is it runs in liner time vs o^n time like going though a for loop for the lenght of N would
In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each elementary operation takes a fixed amount of time to pe...
https://www.youtube.com/watch?v=cWHTyeQg79A, you know this part
The AI alignment problem.
The alignment problem in AI refers to the challenge of designing AI systems with objectives, values, and actions that closely align with human intentions and ethical considerations.
One of AIโs main alignment challenges is its black box nature (inputs and outputs are identifiable but the transformation process in be...
"general soultion in polynomial time" big O notation is baiscaly what hes talking about
finally @hybrid umbra
i couldnt remember the prev logic
so this is false
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
ind=-1
numlist=nums
while len(nums)>1:
ind=ind+1
first=nums[0]
numlist.pop(0)
if (target-first) in numlist:
return [ind, numlist.index(target-first)+ind+1]
return None
still this is slow for some reason...
i remember it now
this is it
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
his={} #his as history
for i, num in enumerate(nums):
x= target - num #x is the valuw we have to find
if x in his:
return [his[x],i]
his[num]=i
try this
def two_sum2(nums, target):
nums.sort()
half_target = target / 2
mid_index = len(nums) // 2
mid_value = nums[mid_index]
if mid_value > half_target:
search_range = nums[:mid_index]
else:
search_range = nums[mid_index:]
for i in range(len(search_range)):
for j in range(i+1, len(search_range)):
if search_range[i] + search_range[j] == target:
return [nums.index(search_range[i]), nums.index(search_range[j])]
return None
print(two_sum2([2, 7, 11, 15], 9))```
run that though the profiler ๐
ohkay one sec
yeah i just realised its kinda a mental test
def two_sum2(nums, target):
indexed_nums = list(enumerate(nums))
indexed_nums.sort(key=lambda x: x[1])
left, right = 0, len(indexed_nums) - 1
while left < right:
current_sum = indexed_nums[left][1] + indexed_nums[right][1]
if current_sum == target:
return [indexed_nums[left][0], indexed_nums[right][0]]
elif current_sum < target:
left += 1
else:
right -= 1
return None
@hybrid umbra also when you click on the bar graph's bar you can see their sample code
[0, 1] [1, 2] [0, 1]
i am good at math
i would love to if you can spare some of your time?
you aint busy right?
ok so what it does, is take the taget value
half it
then it sorts the list
then searches from the midpoint of the list in steps of log2 from the middle of the list
then it has to check the poistion of the new sorted list vs the orginal
and return the values of the orginal list
poitional values
pythons good at list sorting timewise
so its a time effient but not memory effieent method
im just awestruck that you have such knowledge of which takes how much time
experaince
and yeah its dealing with shit like leetcode and testing time
also i deal with big data, sooo i have spent way too much time optimizing code
you work in a job or solo dev?
looking for work, just finished my AI masters
which are the things that it is not good at?
quite alot, however my issue was mainly with stuff like apache spark
there is a python rprofiler tool built in
spend some time with it you will see whats fast whats not
mind if i chat with you for a while in DMS @elfin viper
yes sir
sometime tommrow good? rn i am dealing with making a AI model for alot of data
oh sure sure
ill just ping you in chat and type my stuff, reply to it whenever you can in this week
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.