Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,193,564 members, 7,951,362 topics. Date: Tuesday, 17 September 2024 at 02:44 PM |
Nairaland Forum / Science/Technology / Programming / Thread For Nairaland Algorithm Questions (5752 Views)
Forex Market Algorithm IPDA part 2 / What Is Nairaland Algorithm? / A Thread For Tutorial On Python Programming (2) (3) (4)
(1) (2) (3) (4) (5) (Reply) (Go Down)
Re: Thread For Nairaland Algorithm Questions by naijasensei: 12:59pm On Jan 20, 2021 |
Deicide: number = 12345 def rev_number(num): num_str = str(num) num_str = num_str[::-1] out_num = int(num_str) return out_num print(rev_number(number)) 1 Like
|
Re: Thread For Nairaland Algorithm Questions by Brukx(m): 1:01pm On Jan 20, 2021 |
naijasensei: |
Re: Thread For Nairaland Algorithm Questions by Deicide: 1:08pm On Jan 20, 2021 |
naijasensei:Correct but this code would be very slow & again Python is is a very slow language 1 Like |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 2:23pm On Jan 20, 2021 |
Deicide: Solved NB: please disregard that comment on line 170 , i forgot to remove it when i was testing 1 Like
|
Re: Thread For Nairaland Algorithm Questions by Deicide: 2:44pm On Jan 20, 2021 |
cbrass:You are correct but can you think of a faster way to do it? Since we dealing with Algorithm......one way I can think of is to push into stack and pop it out back, that way it'll come out in reverse other. |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 4:35pm On Jan 20, 2021 |
Deicide: Okay , i will think of another way though this is also fast too |
Re: Thread For Nairaland Algorithm Questions by naijasensei: 4:39pm On Jan 20, 2021 |
Deicide: How about Kotlin then, with two(2) different implementations.
|
Re: Thread For Nairaland Algorithm Questions by Deicide: 6:18pm On Jan 20, 2021 |
naijasensei:Yeah kotlin is very fast |
Re: Thread For Nairaland Algorithm Questions by cixak95211: 7:09pm On Jan 20, 2021 |
You could do a traditional for loop [reverse] which should run in O(n)
|
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 7:41pm On Jan 20, 2021 |
naijasensei: I love this. I did a Google search on switch statement alternative in python and this was my best answer on stackoverflow. 1 Like |
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 7:48pm On Jan 20, 2021 |
airsaylongcome: Any language will do. |
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 7:50pm On Jan 20, 2021 |
Deicide:Why is everyone converting the input to string? Am I missing something? |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 8:03pm On Jan 20, 2021 |
Deicide: is it the language you are most concerned about or the method used ? by the way can we see your own solution |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 8:05pm On Jan 20, 2021 |
Karleb: it is easier that way nw, |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 8:05pm On Jan 20, 2021 |
Next question please |
Re: Thread For Nairaland Algorithm Questions by Deicide: 8:21pm On Jan 20, 2021 |
Karleb:They are using shortcut 1 Like |
Re: Thread For Nairaland Algorithm Questions by Deicide: 8:25pm On Jan 20, 2021 |
cbrass:The method used, though language used also play an important role.....You could also use module arithmetic to solve this, I think that's the fastest way to solve this. |
Re: Thread For Nairaland Algorithm Questions by Brukx(m): 9:47pm On Jan 20, 2021 |
Deicide:Let's see your solution. |
Re: Thread For Nairaland Algorithm Questions by naijasensei: 9:49pm On Jan 20, 2021 |
Deicide: Modulo arithmetic, using the remainder of a division combined with the quotient. Because, given any integer, n: n / divisor = quotient * divisor + remainder. If 10 is our divisor (because it will preserve the number but shift the decimal point to the left, 123/10 = 12.3): Then, n / 10 = quot * 10 + rem. For example, 12345 / 10 = 1234 * 10 + 5. If we divide by 10 repeatedly until we get zero and store the remainder in a variable during each iteration, we can successfully reconstruct the number in reverse by adding the remainders while remembering to multiply each digit by their corresponding powers of 10. reversedNumber = (((rem1 * 10 + rem2) * 10 + rem3) * 10) + ... + remn Image1 - Python Image 2 - Kotlin
|
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 10:52pm On Jan 20, 2021 |
naijasensei: Works like juju. 1 Like |
Re: Thread For Nairaland Algorithm Questions by Donpre(m): 12:15am On Jan 21, 2021 |
naijasensei:Beautiful but....
|
Re: Thread For Nairaland Algorithm Questions by naijasensei: 6:10am On Jan 21, 2021 |
Donpre: Nice observation. There are several things at play here. Your Python interpreter stripped off the leading zero on your text input immediately you cast your input to type int, you can confirm this. If you try to supply an int with a leading zero, your program will not run because it is not a valid int. The main issue I see is, if zero(0) is the last digit on the integer you inputed - it will not appear in your reversed integer. This is because, you will end up having zero as your first integer digit - which is not valid. Therefore, when this solution is used - any integer input that has a zero as the last digit will not produce an integer output with a leading zero. |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 8:53am On Jan 21, 2021 |
naijasensei:then t is solution is not efficient then, it is even slow sef with large numbers |
Re: Thread For Nairaland Algorithm Questions by Deicide: 9:40am On Jan 21, 2021 |
Brukx:
|
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 9:45am On Jan 21, 2021 |
Question. Write a function that determines if a word is a palindrome. Hint: Palindromes are words that has same spelling backward and forward. E.g madam, racecar. |
Re: Thread For Nairaland Algorithm Questions by Brukx(m): 11:08am On Jan 21, 2021 |
Karleb: def checkpalindrum(): 1 Like |
Re: Thread For Nairaland Algorithm Questions by cixak95211: 5:00pm On Jan 21, 2021 |
Question: 1. Design an algorithm(s) for the following array operations: "push, pop, shift, unshift" using object oriented programming practices. 2. Design an algorithm that will find the longest common subsequence between two arrays, plus the time complexity for your solution. |
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 5:29pm On Jan 21, 2021 |
Brukx: |
Re: Thread For Nairaland Algorithm Questions by qtguru(m): 6:10pm On Jan 21, 2021 |
back here this is pcguru1 changed my moniker, there is an algorithm group on LinkedIn for blacks all over the world, anyone care to join, let's face it Nairaland is dead for collaboration. |
Re: Thread For Nairaland Algorithm Questions by qtguru(m): 6:12pm On Jan 21, 2021 |
[quote author=Deicide post=98282750][/quote] Dude that is some sick text based ui, what is window manager is this ? na pcguru1 |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 6:40pm On Jan 21, 2021 |
qtguru:what is the link ? |
Re: Thread For Nairaland Algorithm Questions by qtguru(m): 6:44pm On Jan 21, 2021 |
JSP Web Development / Your Programming Experience And Your Languages. / Who Wants To Learn Python With Me? 90 Days Challenge
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 39 |