Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,567 members, 7,816,392 topics. Date: Friday, 03 May 2024 at 10:35 AM

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

Nairaland Forum / Science/Technology / Programming / Thread For Nairaland Algorithm Questions (5406 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 Sheriman(m): 6:32pm On Jan 23, 2021
cixak95211:


* push
line 23 is not necessary
as you can return the increased length on line 22.

* unshift
line 42 is not necessary
as you can directly assign this.stack to [value, ...this.stack]

depending on some school of thought, it's best to outline each line seperately,
but you'd be wasting memory declaring variables that are really not necessary
a smart code editor e.g. any of the JetBrains family, would have also warned you about it.

Its amazing we have smart minds here, so why did y'all allow this place to be overran
by some "website for 20k" minds?
Lolsss.. Some Nigerians got talent and skills
Re: Thread For Nairaland Algorithm Questions by cixak95211: 6:34pm On Jan 23, 2021
Ques 3:
What is the problem with this block of code?
What is the fix?


const calculateFee= (amount) => {
switch (amount)) {
case 0:
case undefined:
return 0;

case (amount > 0 && amount <= 5000):
return 10;

case amount > 5000 && amount <= 50000:
return 25;

default:
return 50;
}
};
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 6:53pm On Jan 23, 2021
cixak95211:
Ques 3:
What is the problem with this block of code?
What is the fix?


I've not run the code but from what I see.

Case 0 returns nothing. The switch statement will get stucked there.

Solution, let case 0 return something.

case amount > 5000 && amount <= 50000:

I doubt if that is a valid case statement. Javascript will get confused at this stage. The logic should be wrapped around a parenthesis since it involves comparison.
Re: Thread For Nairaland Algorithm Questions by naijasensei: 6:57pm On Jan 23, 2021
cixak95211:


* push
line 23 is not necessary
as you can return the increased length on line 22.

* unshift
line 42 is not necessary
as you can directly assign this.stack to [value, ...this.stack]

depending on some school of thought, it's best to outline each line seperately,
but you'd be wasting memory declaring variables that are really not necessary
a smart code editor e.g. any of the JetBrains family, would have also warned you about it.

Its amazing we have smart minds here, so why did y'all allow this place to be overran
by some "website for 20k" minds?

I agree with not using the additional variable 'temp' on line 42. As for line 23, the redundancy is intentional.

As for the website for 20k guys, I reserve my comments - for now.
Re: Thread For Nairaland Algorithm Questions by Phelumie300: 7:17pm On Jan 23, 2021
lahp:



bro array sizes can never be floats

stop checking if an array is <0

i also think u are to return the new length of array

u should check ur array keys
it says if the key is zero increase the key and add item


have u forgotten an array is indexed from zero?

Yeah thanks for noticing I meant to use ==

@bolded could you be more specific
Re: Thread For Nairaland Algorithm Questions by cixak95211: 7:21pm On Jan 23, 2021
Karleb:


I've not run the code but from what I see.

Case 0 returns nothing. The switch statement will get stucked there.

Solution, let case 0 return something.

case amount > 5000 && amount <= 50000:

I doubt if that is a valid case statement. Javascript will get confused at this stage. The logic should be wrapped around a parenthesis since it involves comparison.


Case 0 returns 0. If you look well, you'll see the cases are stacked which is a valid switch expression
Parenthesis are redundant. A smart code editor, once again, will remove the parentheses if you add it.
Prettier will also remove it.
Try again.
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 7:26pm On Jan 23, 2021
cixak95211:


Case 0 returns 0. If you look well, you'll see the cases are stacked which is a valid switch expression
Parenthesis are redundant. A smart code editor, once again, will remove the parentheses if you add it.
Prettier will also remove it.
Try again.

Let me get my editor. grin
Re: Thread For Nairaland Algorithm Questions by Karleb(m): 7:38pm On Jan 23, 2021
Question, implement the doubly linked list data structure. The implementation should be within a class.
Re: Thread For Nairaland Algorithm Questions by naijasensei: 8:36pm On Jan 23, 2021
cixak95211:
Ques 3:
What is the problem with this block of code?
What is the fix?


From the way switch statements work, the expression within 'switch(expression)' is evaluated only once. That is this arrow function will always return either 0 (whenever amount is zero, or amount is undefined) or 50 for all other values because it ignores the comparison expressions in the two case statements following the first one.
There are two possible solutions I can think of:
1. Use an if...else if...else construct.
2. Make the expression within 'switch(expr)' to always evaluate to true, then check your expressions within your case statements.

const calculateFee = (amount) => {
switch (true) {
case amount === 0:
case amount === undefined:
return 0;

case (amount > 0 && amount <= 5000):
return 10;

case (amount > 5000 && amount <= 50000):
return 25;

default:
return 50;
}
};

const calculateFeeAlt = amount => {
if (amount === 0 || amount === undefined) {
return 0;
} else if (amount > 0 && amount <= 5000) {
return 10;
} else if (amount > 5000 && amount <= 50000) {
return 25;
} else {
return 50;
}
};

Re: Thread For Nairaland Algorithm Questions by cixak95211: 8:50pm On Jan 23, 2021
@naijasensei:

Nailed it!!!! However
case amount === 0:
case amount === undefined:

can be better rewritten as case !amount since ! will evaluate the same for falsey, null, zero and undefined

same goes for amount === 0 || amount === undefined.
Re: Thread For Nairaland Algorithm Questions by lahp(m): 12:09am On Jan 30, 2021
question: create a linked list data structure using OOP
Re: Thread For Nairaland Algorithm Questions by Grandlord: 9:22am On Jan 30, 2021
Minds at work cool

This is my hobby wink
Re: Thread For Nairaland Algorithm Questions by Nobody: 9:43am On Jan 30, 2021
Shouldn't there be a standard algorithm syntax that explains the problem well? If there isn't then this threads name is bogus.
Re: Thread For Nairaland Algorithm Questions by bunnae(f): 10:05pm On Jan 30, 2021
Grandlord:
Minds at work cool

This is my hobby wink
GrandLord, where you enter since? Happy new year
Re: Thread For Nairaland Algorithm Questions by logicDcoder(m): 12:32am On Jan 31, 2021
Re: Thread For Nairaland Algorithm Questions by Grandlord: 8:54am On Jan 31, 2021
bunnae:

Grand.Lord, where you enter since? Happy new year
Bunnae my baby girl smiley

E don Tay sha. How ya side na?

Wey our man icode2? Be like e don japa o cheesy
Re: Thread For Nairaland Algorithm Questions by Grandlord: 8:56am On Jan 31, 2021
SegFault:
Shouldn't there be a standard algorithm syntax that explains the problem well? If there isn't then this threads name is bogus.
Do you mean pseudocodes and flowcharts?

Na extra work o cheesy

1 Like

Re: Thread For Nairaland Algorithm Questions by Nobody: 8:58am On Jan 31, 2021
Grandlord:

Do you mean pseudocodes and flowcharts?

Na extra work o cheesy
Only pseudocodes are enough, can't be reading code I don't understand.

1 Like

Re: Thread For Nairaland Algorithm Questions by Grandlord: 9:05am On Jan 31, 2021
SegFault:

Only pseudocodes are enough, can't be reading code I don't understand.
I think pseudocodes will be too much to ask for here.

Writing comments should be okay.
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 12:51pm On Feb 01, 2021
Karleb:


Please explain this hint more.

It seems you want a deeper level of PQ. as in, a PQ in a PQ in a PQ... undecided

Yea i aught to give you examples sef. actually this question not mine i just feel led to post
Re: Thread For Nairaland Algorithm Questions by cbrass(m): 12:52pm On Feb 01, 2021
Grandlord:

I think pseudocodes will be too much to ask for here.

Writing comments should be okay.

Comments is fine
Re: Thread For Nairaland Algorithm Questions by Brukx(m): 12:20am On Feb 06, 2021
Exercise
Write a pagination function. This function will convert a 1 dimensional array into a paginated 2 dimensional array. It will take 2 arguments. The first argument is the array to be paginated and the second one is the number of elements per pagination.

Examples
list=[1,2,3,4,5,6,7,8,9,10]

paginate(list, 5) should return
[[1,2,3,4,5], [6,7,8,9,10]]

paginate(list,2) should return
[[1,2], [3,4], [5,6], [7,8], [9,10]]

paginate(list, 3) should return
[[1,2,3], [4,5,6], [7,8,9], [10]]
Re: Thread For Nairaland Algorithm Questions by Nobody: 12:53am On Feb 06, 2021
who will teach me programming oo
Re: Thread For Nairaland Algorithm Questions by Deicide: 3:45pm On Jul 03, 2021
You are given an array and asked to find the element in the array that appears k times.

eg

arr = {1, 2, 2, 3, 3,3,4,4,4,4}

fun = foo(arr, k) k = 2

ans = 2
Re: Thread For Nairaland Algorithm Questions by JFOD: 9:03pm On Jul 03, 2021
Brukx:
Exercise
Write a pagination function. This function will convert a 1 dimensional array into a paginated 2 dimensional array. It will take 2 arguments. The first argument is the array to be paginated and the second one is the number of elements per pagination.

Examples
list=[1,2,3,4,5,6,7,8,9,10]

paginate(list, 5) should return
[[1,2,3,4,5], [6,7,8,9,10]]

paginate(list,2) should return
[[1,2], [3,4], [5,6], [7,8], [9,10]]

paginate(list, 3) should return
[[1,2,3], [4,5,6], [7,8,9], [10]]



def page(lst, move):
length = len(lst)
output = [ ]
for i in range(0, length,move):
if i+ move < length:
output.append(lst[i: i+move])
else:
output.append(lst[i:])

return output

print(page(list,5))

Re: Thread For Nairaland Algorithm Questions by JFOD: 12:26am On Jul 04, 2021
Deicide:
You are given an array and asked to find the element in the array that appears k times.

eg

arr = {1, 2, 2, 3, 3,3,4,4,4,4}

fun = foo(arr, k) k = 2

ans = 2


Hmm, if I'm to solve this

First attempt:
Get the unique numbers
loop through it
get the count of each number
Append it to a list /print it out if the count = 2
Re: Thread For Nairaland Algorithm Questions by Deicide: 1:06am On Jul 04, 2021
JFOD:


Hmm, if I'm to solve this

First attempt:
Get the unique numbers
loop through it
get the count of each number
Append it to a list /print it out if the count = 2

Ok let's see implementation
Re: Thread For Nairaland Algorithm Questions by JFOD: 1:18am On Jul 04, 2021
Deicide:
You are given an array and asked to find the element in the array that appears k times.

eg

arr = {1, 2, 2, 3, 3,3,4,4,4,4}

fun = foo(arr, k) k = 2

ans = 2


I'll make use of python (it's easy to get the unique numbers using set)

I'm assuming, list will be passed


def findK(lst, x):
unique_num = {lst}
num_with_x_occurence = [ ]

for i in unique_num:
occurrence = lst.count(i)

if occurrence == x:
num_with_x_occurence.append(i)

return num_with_x_occurence

Re: Thread For Nairaland Algorithm Questions by Deicide: 1:49pm On Jul 04, 2021
JFOD:


I'll make use of python (it's easy to get the unique numbers using set)

I'm assuming, list will be passed


def findK(lst, x):
unique_num = {lst}
num_with_x_occurence = [ ]

for i in unique_num:
occurrence = lst.count(i)

if occurrence == x:
num_with_x_occurence.append(i)

return num_with_x_occurence

did you test this code? am getting error for unique_num = {lst} unhashable type. though the solutions is correct. but then answer wouldn't be unique cause I have to remove {}
Re: Thread For Nairaland Algorithm Questions by JFOD: 2:16pm On Jul 04, 2021
Deicide:
did you test this code? am getting error for unique_num = {lst} unhashable type. though the solutions is correct. but then answer wouldn't be unique cause I have to remove {}

I did not test the code.

how to fix it

#this will convert the list to set (set doesn't #allow duplicate values, so it will only have #unique numbers)

unique_num = set(lst)
Re: Thread For Nairaland Algorithm Questions by sharrp: 2:43pm On Jul 04, 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

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.

You asked a wrong question , provided a wrong answer and with wrong input
Re: Thread For Nairaland Algorithm Questions by sharrp: 2:44pm On Jul 04, 2021
Karleb:


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

Just because he used the word pop and push doesn’t mean this requires a stack

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