Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,589 members, 7,816,453 topics. Date: Friday, 03 May 2024 at 11:27 AM

Thread For Nairaland Algorithm Questions - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Thread For Nairaland Algorithm Questions (5407 Views)

Public API For Nairaland / 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:
Question
Given an input number n, you are to reverse the number.
E.G
Input - 12345
Output - 54321

Input - 18352
Output - 25381

Input - 2
Output - 2

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:


my_input = input("Enter a number, either 4 or 7: "wink

my_output = {'4': 7, '7': 4}

def is_it_4_or_7(my_var):
return my_output.get(my_var)

print(is_it_4_or_7(my_input))
cool
Re: Thread For Nairaland Algorithm Questions by Deicide: 1:08pm On Jan 20, 2021
naijasensei:


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))
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:
Question
Given an input number n, you are to reverse the number.
E.G
Input - 12345
Output - 54321

Input - 18352
Output - 25381

Input - 2
Output - 2

Solved
NB: please disregard that comment on line 170 , i forgot to remove it when i was testing cheesy

1 Like

Re: Thread For Nairaland Algorithm Questions by Deicide: 2:44pm On Jan 20, 2021
cbrass:


Solved
NB: please disregard that comment on line 170 , i forgot to remove it when i was testing cheesy
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:
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.

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:
Correct but this code would be very slow & again Python is is a very slow language

How about Kotlin then, with two(2) different implementations.

Re: Thread For Nairaland Algorithm Questions by Deicide: 6:18pm On Jan 20, 2021
naijasensei:


How about Kotlin then, with two(2) different implementations.
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)


const reverse = (a) => {
let val = String(a), result = '';

for (let i = val.length - 1; i >= 0; i--) {
result += val[i];
}

return +result;
}
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 7:41pm On Jan 20, 2021
naijasensei:


my_input = input("Enter a number, either 4 or 7: "wink

my_output = {'4': 7, '7': 4}

def is_it_4_or_7(my_var):
return my_output.get(my_var)

print(is_it_4_or_7(my_input))

I love this. I did a Google search on switch statement alternative in python and this was my best answer on stackoverflow. cool

1 Like

Re: Thread For Nairaland Algorithm Questions by Karleb(m): 7:48pm On Jan 20, 2021
airsaylongcome:


Case-Switch as I know them. Would be nice if you guys can indicate what language you want the solution in. Some of us are from the very old school of programming. I'm talking BASIC and Pascal and even LISP

Any language will do.
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 7:50pm On Jan 20, 2021
Deicide:
Question Given an input number n, you are to reverse the number. E.G Input - 12345 Output - 54321
Input - 18352 Output - 25381
Input - 2 Output - 2
Why is everyone converting the input to string?
Am I missing something? undecided
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 8:03pm On Jan 20, 2021
Deicide:
Yeah kotlin is very fast

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:


Why is everyone converting the input to string?

Am I missing something? undecided

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:


Why is everyone converting the input to string?

Am I missing something? undecided
They are using shortcut grin

1 Like

Re: Thread For Nairaland Algorithm Questions by Deicide: 8:25pm On Jan 20, 2021
cbrass:


is it the language you are most concerned about or the method used ?

by the way can we see your own solution
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:
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.
Let's see your solution.
Re: Thread For Nairaland Algorithm Questions by naijasensei: 9:49pm On Jan 20, 2021
Deicide:
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.

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:


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



Works like juju.

1 Like

Re: Thread For Nairaland Algorithm Questions by Donpre(m): 12:15am On Jan 21, 2021
naijasensei:


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


Beautiful but....

Re: Thread For Nairaland Algorithm Questions by naijasensei: 6:10am On Jan 21, 2021
Donpre:

Beautiful but....

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:


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.
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:
Let's see your solution.

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:
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.

def checkpalindrum():
x=input("Enter a word:" )
a=0
b=-1
while x[a]==x[b]:
length=len(x)
a+=1
b-=1
if a==length:
return x+" is a palindrom"

else:
return x+" is not a palindrom"


print(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:

def checkpalindrum():
 	x=input("Enter a word:" )
 	a=0
 	b=-1
 	while x[a]==x[b]:
 		length=len(x)
 		a+=1
 		b-=1
 		if a==length:
 			return x+" is a palindrom"
else: return x+" is not a palindrom"

print(checkpalindrum())
cool
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:
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.
what is the link ?
Re: Thread For Nairaland Algorithm Questions by qtguru(m): 6:44pm On Jan 21, 2021
cbrass:

what is the link ?

I'll share soon

https://www.linkedin.com/groups/8873861

(1) (2) (3) (4) (5) (Reply)

Programming Challenge: Convert String to Json with a Loop / List Of Failed Software Projects - What Went Wrong? / Discover How To Be A Blockchain Developer & Blockchain Engineer

(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. 40
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.