Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,193,556 members, 7,951,321 topics. Date: Tuesday, 17 September 2024 at 02:10 PM |
Nairaland Forum / Science/Technology / Programming / Thread For Nairaland Algorithm Questions (5747 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)
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 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, 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: 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: 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:What is lol... |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 11:54am On Jan 19, 2021 |
SqueezedPant: Hackerrank or Leetcode is fine |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 11:55am On Jan 19, 2021 |
Karleb: not really bro... |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 11:58am On Jan 19, 2021 |
meobizy:why laughing nw |
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 12:54pm On Jan 19, 2021 |
cbrass: 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. |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 5:07pm On Jan 19, 2021 |
Karleb: 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: So take a positive integer, push it into the array, pop the same integer and return it. Easy. 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: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: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: 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: Ah!! Karleb have you seen this question? we have crazy questions on Nairaland o I am loving this. SO no conditional statement at all? |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 10:41pm On Jan 19, 2021 |
Brukx: But you used IF in your question nw 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: 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: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: 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: 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: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: 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:The function should not run if 4 or 7 is passed. user_input = input() 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: 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
1 Like |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 9:51am On Jan 20, 2021 |
Karleb: but he says he doesn't want conditional statement nw. when last did i use switch sef |
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 9:52am On Jan 20, 2021 |
Deicide: 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: my_input = input("Enter a number, either 4 or 7: " 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
|
Functional Programming For Java Programmer Scala Or Clojure? / JSP Web Development / Your Programming Experience And Your Languages.
(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. 50 |