Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,629 members, 7,813,085 topics. Date: Tuesday, 30 April 2024 at 06:41 AM

Thread For Nairaland Algorithm Questions - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Thread For Nairaland Algorithm Questions (5392 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)

Thread For Nairaland Algorithm Questions by cbrass(m): 3:03am On Jan 19, 2021
Hi my fellow coders, i think we should have our own mini Hackerank or Leetcode here too.
I just feel like we should challenge our selves by posting OUR OWN algorithm questions here.

Please let these questions BE YOUR OWN QUESTIONS and not copy and paste from other sites!!

I will suggest that we let a question be answered before posting another one,
If a question is not solved within a day the questioner will modify the post to include the solution.

I will go first grin

3 Likes

Re: Thread For Nairaland Algorithm Questions by cbrass(m): 3:03am On Jan 19, 2021
Write a function that returns the maximum number N of an array using the following functions POP and PUSH in your code
HINT: Input = 15, Output = 15, input = 10 Output = 10

Modified
Please find the image below for the solution.
I purposely didn't want to give more hint, cheesy
From the question you aught to create an array from the given input.

2 Likes

Re: Thread For Nairaland Algorithm Questions by meobizy(f): 8:16am On Jan 19, 2021
Lol.
Re: Thread For Nairaland Algorithm Questions by SqueezedPant: 8:52am On Jan 19, 2021
cbrass:
Write a function that returns the maximum number N of an array using the following functions POP and PUSH in your code
HINT: Input = 15, Output = 15


Good morning ,I've been looking for a site where I can see questions like this to improve my JavaScript skill , can you suggest some?
thanks in advance
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 9:10am On Jan 19, 2021
cbrass:
Write a function that returns the maximum number N of an array using the following functions POP and PUSH in your code
HINT: Input = 15, Output = 15

I'm guessing this will require a stack data structure?
Re: Thread For Nairaland Algorithm Questions by Nobody: 9:28am On Jan 19, 2021
meobizy:
Lol.
What is lol... undecided
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 11:54am On Jan 19, 2021
SqueezedPant:



Good morning ,I've been looking for a site where I can see questions like this to improve my JavaScript skill , can you suggest some?
thanks in advance

Hackerrank or Leetcode is fine
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 11:55am On Jan 19, 2021
Karleb:


I'm guessing this will require a stack data structure?

not really bro... wink
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 11:58am On Jan 19, 2021
meobizy:
Lol.
why laughing nw
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 12:54pm On Jan 19, 2021
cbrass:


not really bro... wink

The only method I know does not allow push/pop, the one that does is a whole stack object.

Give more hints.

Edit.

I just thought of something, a sort algorithm would solve this. Sort the array in asc or desc order and pop out the last or first value. Even if in future another value is pushed to the array, the sort algorithm will take care of the order.

Is this the solution? Lemme write it.

Re: Thread For Nairaland Algorithm Questions by Karleb(m): 1:54pm On Jan 19, 2021
Next question please. grin
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 5:07pm On Jan 19, 2021
Karleb:


The only method I know does not allow push/pop, the one that does is a whole stack object.

Give more hints.

Edit.

I just thought of something, a sort algorithm would solve this. Sort the array in asc or desc order and pop out the last or first value. Even if in future another value is pushed to the array, the sort algorithm will take care of the order.

Is this the solution? Lemme write it.

well you were coming close to the solution until i saw your code. don't forget the input is not an Array..the input is a positive integer just like the hint i gave annd you must return an interger too..in fact from the hint you should already know how the ALgorithm will be
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 5:21pm On Jan 19, 2021
cbrass:


well you were coming close to the solution until i saw your code. don't forget the input is not an Array..the input is a positive integer just like the hint i gave annd you must return an interger too..in fact from the hint you should already know how the ALgorithm will be

So take a positive integer, push it into the array, pop the same integer and return it. Easy. grin

function returnInteger(value) {
let arr = []
arr. push(value)

return arr.pop()
}
Re: Thread For Nairaland Algorithm Questions by Deicide: 5:55pm On Jan 19, 2021
cbrass:
Write a function that returns the maximum number N of an array using the following functions POP and PUSH in your code
HINT: Input = 15, Output = 15, input = 10 Output = 10
If I get this Question correctly, You are asking to find the maximum element in an Array?

You also said to use pop and push, that doesn't make any sense cause you only use that with Stack data structure and not Arrays.

This Question doesn't even require Finding max of Anything based on the input data
Re: Thread For Nairaland Algorithm Questions by Brukx(m): 9:58pm On Jan 19, 2021
Lemme just drop this here

Question
Write a function that takes user input. The input must be either 7 or 4. If the input is 4, the function should return 7. If the input is 7, the function should return 4. Do not use if or else statements. Do not use exceptions.
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 10:15pm On Jan 19, 2021
Deicide:
If I get this Question correctly, You are asking to find the maximum element in an Array?

You also said to use pop and push, that doesn't make any sense cause you only use that with Stack data structure and not Arrays.

This Question doesn't even require Finding max of Anything based on the input data
i have a reason for asking that you use pop and push, you can use this in array manipulations though and also i dont want to give too much hint so the question doesn't look to easy though it is very easy

1 Like

Re: Thread For Nairaland Algorithm Questions by cbrass(m): 10:32pm On Jan 19, 2021
Karleb:


So take a positive integer, push it into the array, pop the same integer and return it. Easy. grin

function returnInteger(value) {
let arr = []
arr. push(value)

return arr.pop()
}


grin grin grin grin grin
see my solution, though you are right too, you got me there
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 10:36pm On Jan 19, 2021
Brukx:
Lemme just drop this here

Question
Write a function that takes user input. The input must be either 7 or 4. If the input is 4, the function should return 7. If the input is 7, the function should return 4. Do not use if or else statements. Do not use exceptions.

Ah!! Karleb have you seen this question? we have crazy questions on Nairaland o grin grin
I am loving this.
SO no conditional statement at all? embarassed
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 10:41pm On Jan 19, 2021
Brukx:
Lemme just drop this here

Question
Write a function that takes user input. The input must be either 7 or 4. If the input is 4, the function should return 7. If the input is 7, the function should return 4. Do not use if or else statements. Do not use exceptions.

But you used IF in your question nw cheesy cheesy

we need to do compare input , so how will that be possible without using statements
Re: Thread For Nairaland Algorithm Questions by JFOD: 11:19pm On Jan 19, 2021
Brukx:
Lemme just drop this here

Question
Write a function that takes user input. The input must be either 7 or 4. If the input is 4, the function should return 7. If the input is 7, the function should return 4. Do not use if or else statements. Do not use exceptions.


Create a dictionary/object
Use 7 as a key and 4 has a value (vice versa)
Create a function that will take one argument
Return the dictionary index using the argument that was passed.

Python implementation

Unamed_variable= {7:4,4:7}

def dont_use_if(x):
return Unamed_variable[x]

This should work

Modified:

Some measures are not put in place
Example: it should only accept either 4 or 7

While loop comes to mind but I feel that's not a good idea.

def is_7(y):
return y == 7


def is_4(y):
return y == 4

Then add this to the first function

a = is_7(x)
b = is_4(x)

while a or b:
return unamed_variabele[x]
break

This should work fine

1 Like

Re: Thread For Nairaland Algorithm Questions by Brukx(m): 12:27am On Jan 20, 2021
JFOD:



Create a dictionary/object
Use 7 as a key and 4 has a value (vice versa)
Create a function that will take one argument
Return the dictionary index using the argument that was passed.

Python implementation

Unamed_variable= {7:4,4:7}

def dont_use_if(x):
return Unamed_variable[x]

This should work

Modified:

Some measures are not put in place
Example: it should only accept either 4 or 7

While loop comes to mind but I feel that's not a good idea.

def is_7(y):
return y == 7


def is_4(y):
return y == 4

Then add this to the first function

a = is_7(x)
b = is_4(x)

while a or b:
return unamed_variabele[x]
break

This should work fine
I'm seeing 3 functions here. Where is the main function ? I'm also not seeing any point where user input is requested.
Re: Thread For Nairaland Algorithm Questions by JFOD: 12:39am On Jan 20, 2021
Brukx:

I'm seeing 3 functions here. Where is the main function ? I'm also not seeing any point where user input is requested.

user_input = input()

paired_num = {'7':4, '4':7}
#checks if the number is 7, returns True if it is
def is_7(y):
return '7'==y

def is_4(y):
return '4'==y

def main_func(x):
seven = is_7(x)
four = is_4(x)
while seven or four:
return paired_num[x]
break

print(main_func(user_input))

Note: I worked with strings because python takes user input as strings and I don't want to do type conversion cox that will break it if a non digit character is passed.

What do you think? Good enough?

Modified

I skipped the user input part in the first post because I felt it wasn't necessary
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 1:09am On Jan 20, 2021
cbrass:


But you used IF in your question nw cheesy cheesy

we need to do compare input , so how will that be possible without using statements

One simple way is to use switch cases.

1 Like

Re: Thread For Nairaland Algorithm Questions by Brukx(m): 1:12am On Jan 20, 2021
JFOD:


user_input = input()

paired_num = {'7':4, '4':7}
#checks if the number is 7, returns True if it is
def is_7(y):
return '7'==y

def is_4(y):
return '4'==y

def main_func(x):
seven = is_7(x)
four = is_4(x)
while seven or four:
return paired_num[x]
break

print(main_func(user_input))

Note: I worked with strings because python takes user input as strings and I don't want to do type conversion cox that will break it if a non digit character is passed.

What do you think? Good enough?

Modified

I skipped the user input part in the first post because I felt it wasn't necessary
Almost complete but the main function is accepting arguments outside 4 and 7
Re: Thread For Nairaland Algorithm Questions by JFOD: 1:21am On Jan 20, 2021
Brukx:
Almost complete but the main function is accepting arguments outside 4 and 7

Yes, it is . However it's not going to do anything

Maybe I don't really understand, what should happen if something other than 4 or 7 is passed?
Re: Thread For Nairaland Algorithm Questions by Brukx(m): 7:23am On Jan 20, 2021
JFOD:


Yes, it is . However it's not going to do anything

Maybe I don't really understand, what should happen if something other than 4 or 7 is passed?
The function should not run if 4 or 7 is passed.

user_input = input()

paired_num = {'7':4, '4':7}
#checks if the number is 7, returns True if it is
def is_7(y):
return '7'==y

def is_4(y):
return '4'==y

seven = is_7(user_input)
four = is_4(user_input)
def main_func(x):
return paired_num[x]

while seven or four:
print(main_func(user_input))
break


This will not return None if another number is passed as an argument
Re: Thread For Nairaland Algorithm Questions by airsaylongcome: 7:32am On Jan 20, 2021
Karleb:


One simple way is to use switch cases.

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
Re: Thread For Nairaland Algorithm Questions by Deicide: 7:36am On Jan 20, 2021
Lemme use java jare
 
//boiler plates here....
public static void main(String[] args){
Scanner SC = new Scanner(System.in);
int x = SC.nextInt();
switch(x){
case 7:
System.out.println(4);
break;
case 4:
System.out.println(7);
break;
default:
throw new IllegalArgumentException();
//Can also print( "Try again" );
}
}

1 Like

Re: Thread For Nairaland Algorithm Questions by cbrass(m): 9:51am On Jan 20, 2021
Karleb:


One simple way is to use switch cases.

but he says he doesn't want conditional statement nw.
when last did i use switch sef embarassed
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 9:52am On Jan 20, 2021
Deicide:
Lemme use java jare
 
//boiler plates here....
public static void main(String[] args){
Scanner SC = new Scanner(System.in);
int x = SC.nextInt();
switch(x){
case 7:
System.out.println(4);
break;
case 4:
System.out.println(7);
break;
default:
throw new IllegalArgumentException();
//Can also print( "Try again" );
}
}



correct
Re: Thread For Nairaland Algorithm Questions by Deicide: 11:46am On Jan 20, 2021
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

1 Like

Re: Thread For Nairaland Algorithm Questions by naijasensei: 12:48pm On Jan 20, 2021
Brukx:
Lemme just drop this here

Question
Write a function that takes user input. The input must be either 7 or 4. If the input is 4, the function should return 7. If the input is 7, the function should return 4. Do not use if or else statements. Do not use exceptions.

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))

1 Like

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

Video Game Developers Nigeria [artists/animators, Programmers, Sound People] / I Want To Learn Programming - Is Java The Best To Start With? / Programming Challenge: Convert String to Json with a Loop

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