Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,151,052 members, 7,810,930 topics. Date: Saturday, 27 April 2024 at 06:35 PM

A Thread For Competitive Programming - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / A Thread For Competitive Programming (1828 Views)

Competitive Programming / Thread For Competitive Programming / A Thread For Tutorial On Python Programming (2) (3) (4)

(1) (2) (Reply) (Go Down)

A Thread For Competitive Programming by logicDcoder(m): 10:00pm On Jan 28, 2021
Good Morning NL!

This thread is meant for programmers [noob] that want to sharpen their problem solving skills using any programming language. The OP of the thread will be posting two/three algorithmic problems here every week.

READ ME: Anybody can contribute his/her own solution but make sure to provide the pseudo code. You can use any programming language of your choice. To submit your solution screenshot your IDE and post the resulting image here(A well commented code don't need a pseudo code). Scroll Down To See Week 2 Questions.

Thread Official Languages: English/Pidgin English

NB: Try and solve the problems first before consulting Mr Stackoverflow.

Ogah Seun, Mukina2 and lalasticlala let's give this thread a chance *smiles*

Thanks
Okeke Enoch Chibuzo

2 Likes

Re: A Thread For Competitive Programming by logicDcoder(m): 10:18pm On Jan 28, 2021
WEEK 1:
Well will start from the easy ones:

1 Like

Re: A Thread For Competitive Programming by tensazangetsu20(m): 10:32pm On Jan 28, 2021
This is for the gods of programming. I don japa grin grin.

1 Like

Re: A Thread For Competitive Programming by logicDcoder(m): 10:41pm On Jan 28, 2021
Problem 1 Solution:

PL used: Java

The approach was quite easy.
Pseudo Code
[1] Iterate the array and multiply the adjacent elements and then compare the product with the variable containing the max product.
[2]If the product of the adjacent element is greater than that of the variable containing the max product then assign the value of the product of the adjacent element to the variable containing the max product.

1 Like

Re: A Thread For Competitive Programming by logicDcoder(m): 10:42pm On Jan 28, 2021
tensazangetsu20:
This is for the gods of programming. I don japa grin grin.

Where you dey go?... Relax and learn

2 Likes

Re: A Thread For Competitive Programming by 404Dev: 10:33am On Jan 29, 2021
The solution in javascript.

Well commented so newbies can follow through.

We could eliminate the first if check but I just left it there so it would be easier for newbies to follow along.

const arr = [3, 6, -2, -5, 7, 3]

/**
* Returns the max product of adjacent elements of input array
* @param inputArray
* @returns {number}
*/
function adjacentElementsProducts(inputArray) {
// Define our max number and initialize it as zero
let max = 0;

// Loop through the input array
for (let i = 0; i < inputArray.length; i++) {
// If this is the first iteration set max to the first element * the next element
if (i == 0) {
max = i * inputArray[i + 1];
} else {
// First check if we have a next element and compare the product of the next two elements
if ((i + 1) != inputArray.length && (i * inputArray[i + 1]) > max) {
// set our max if applicable
max = i * inputArray[i + 1];
}
}
}

return max;
}


console.log(adjacentElementsProducts(arr));

Re: A Thread For Competitive Programming by 404Dev: 10:36am On Jan 29, 2021
tensazangetsu20:
This is for the gods of programming. I don japa grin grin.
Not difficult you can check my solution.
Re: A Thread For Competitive Programming by Grandlord: 2:07pm On Jan 29, 2021
logicDcoder:


Where you dey go?... Relax and learn

Make we reason time complexity for we solutions? If na so abeg indicate the big O wey our solution suppose run. I know say na mostly O(N) sha.


Abeg wey you get the questions from?
Re: A Thread For Competitive Programming by logicDcoder(m): 6:02pm On Jan 29, 2021
Grandlord:


Make we reason time complexity for we solutions? If na so abeg indicate the big O wey our solution suppose run. I know say na mostly O(N) sha.


Abeg wey you get the questions from?

You don't need to know any of this when solving this kind of problem here in nairaland. CP sites like Topcoder, CodeSignal , Hackerrank etc requires the knowledge of time complexity. Bro just study each problem and try and solve 'em.(Here you don't have any time or memory limit).

I got it from CodeSignal

1 Like

Re: A Thread For Competitive Programming by Karlebolu(m): 11:31pm On Jan 29, 2021
I hope this thread doesn't die like others.
Re: A Thread For Competitive Programming by logicDcoder(m): 12:11am On Jan 30, 2021
Karlebolu:
I hope this thread doesn't die like others.
Help the thread by solving a problem
Re: A Thread For Competitive Programming by lahp(m): 12:20am On Jan 30, 2021
logicDcoder:
Okay Let's Go
Lemme Use Pidgin English, I think it will spice up the whole thing. Nairalanders una well done ooo, I go dey post 3 programming wahalas here every week make we programmers dey use am dey warm up our brain. We go first start from the small small ones and then we go progress to harder ones.

You can use any programming language of your choice.

To submit your solution screenshot your IDE and post the resulting image here.

Thread Official Languages: English/Pidgin English

NB: Try and solve the problems first before consulting Mr Stackoverflow.

Ogah Mukina2 let's give this thread a chance *smiles*

no more questions?
Re: A Thread For Competitive Programming by Brightale(f): 1:13am On Jan 30, 2021
404Dev:
The solution in javascript.

Well commented so newbies can follow through.

We could eliminate the first if check but I just left it there so it would be easier for newbies to follow along.

const arr = [3, 6, -2, -5, 7, 3]

/**
* Returns the max product of adjacent elements of input array
* @param inputArray
* @returns {number}
*/
function adjacentElementsProducts(inputArray) {
// Define our max number and initialize it as zero
let max = 0;

// Loop through the input array
for (let i = 0; i < inputArray.length; i++) {
// If this is the first iteration set max to the first element * the next element
if (i == 0) {
max = i * inputArray[i + 1];
} else {
// First check if we have a next element and compare the product of the next two elements
if ((i + 1) != inputArray.length && (i * inputArray[i + 1]) > max) {
// set our max if applicable
max = i * inputArray[i + 1];
}
}
}

return max;
}


console.log(adjacentElementsProducts(arr));
Hi sir, please can you explain to me why JavaScript code is not working on all my browsers. JavaScript has been enabled. I do embed the JavaScript into my HTML codes, then test on browsers. The HTML and CSS codes do work, but JavaScript don't
Re: A Thread For Competitive Programming by logicDcoder(m): 1:14pm On Jan 30, 2021
lahp:


no more questions?
Three per week
Re: A Thread For Competitive Programming by logicDcoder(m): 6:24pm On Jan 30, 2021
Second Problem. It's quite easy all you have to do is convert, extract and then sum.

cc: lahp 404Dev Grandlord

Re: A Thread For Competitive Programming by tensazangetsu20(m): 8:52pm On Jan 30, 2021
Brightale:
Hi sir, please can you explain to me why JavaScript code is not working on all my browsers. JavaScript has been enabled. I do embed the JavaScript into my HTML codes, then test on browsers. The HTML and CSS codes do work, but JavaScript don't
You might not be linking the JavaScript in the HTML file.
Re: A Thread For Competitive Programming by logicDcoder(m): 1:15am On Jan 31, 2021
404Dev:
The solution in javascript.

Well commented so newbies can follow through.

We could eliminate the first if check but I just left it there so it would be easier for newbies to follow along.

const arr = [3, 6, -2, -5, 7, 3]

/**
* Returns the max product of adjacent elements of input array
* @param inputArray
* @returns {number}
*/
function adjacentElementsProducts(inputArray) {
// Define our max number and initialize it as zero
let max = 0;

// Loop through the input array
for (let i = 0; i < inputArray.length; i++) {
// If this is the first iteration set max to the first element * the next element
if (i == 0) {
max = i * inputArray[i + 1];
} else {
// First check if we have a next element and compare the product of the next two elements
if ((i + 1) != inputArray.length && (i * inputArray[i + 1]) > max) {
// set our max if applicable
max = i * inputArray[i + 1];
}
}
}

return max;
}


console.log(adjacentElementsProducts(arr));

Hope you tested your code with different arrays
Re: A Thread For Competitive Programming by Excallibur(m): 1:43am On Jan 31, 2021
on Python

Re: A Thread For Competitive Programming by logicDcoder(m): 1:47am On Jan 31, 2021
Excallibur:
on Python

Great!
Re: A Thread For Competitive Programming by Brightale(f): 11:16am On Jan 31, 2021
tensazangetsu20:

You might not be linking the JavaScript in the HTML file.
The JavaScript is in the html
Re: A Thread For Competitive Programming by Brightale(f): 11:18am On Jan 31, 2021
tensazangetsu20:

You might not be linking the JavaScript in the HTML file.
is it possible to link with a PC, without connection to the internet. I'm still studying so, all I do is without internet, I write the codes(HTML, and CSS and JavaScript) in the word pad, then go to chrome or firefox to open, only the HTML and CSS codes are the ones working
Re: A Thread For Competitive Programming by tensazangetsu20(m): 11:23am On Jan 31, 2021
Brightale:
is it possible to link with a PC, without connection to the internet. I'm still studying so, all I do is without internet, I write the codes(HTML, and CSS and JavaScript) in the word pad, then go to chrome or firefox to open, only the HTML and CSS codes are the ones working
Screenshot your HTML and JavaScript file let me see.
Re: A Thread For Competitive Programming by Brightale(f): 12:28pm On Jan 31, 2021
tensazangetsu20:

Screenshot your HTML and JavaScript file let me see.
send me your WhatsApp number please, it's not sending here!
Re: A Thread For Competitive Programming by SUPREMOTM: 3:55pm On Jan 31, 2021
QUESTION 1

Language C#

First screenshot is the code in Visual Studio.

MaximumProductOfAdjacentValues() method is used to obtain the result.

buttonMax_Click() method is used to call the above method when the button is clicked.

I turned it to a desktop application, so you get the results displayed whenever you click a button.

The 3 screenshots below it are examples and the displayed results.

1 Like

Re: A Thread For Competitive Programming by SUPREMOTM: 4:05pm On Jan 31, 2021
QUESTION 2

Language C#

timeOfDay() method is used to obtain the result.

buttonGetTime_Click() method is used to call the above method when the button is clicked.

I turned it to a desktop application, so you get the results displayed whenever you click a button.

The 3 screenshots below it are examples and the obtained results.

1 Like

Re: A Thread For Competitive Programming by iAmChidiebere: 12:40pm On Feb 01, 2021
You guys are doing well ooin.
First problem.
Python

1 Like

Re: A Thread For Competitive Programming by iAmChidiebere: 12:41pm On Feb 01, 2021
Python
Second problem.

1 Like

Re: A Thread For Competitive Programming by SUPREMOTM: 3:19pm On Feb 01, 2021
QUESTION 1

Language: JavaScript

IDE used: Bracket

Tested on Chrome browser with the displayed results

Re: A Thread For Competitive Programming by Hamzasaid(m): 3:31pm On Feb 01, 2021
Brightale:
is it possible to link with a PC, without connection to the internet. I'm still studying so, all I do is without internet, I write the codes(HTML, and CSS and JavaScript) in the word pad, then go to chrome or firefox to open, only the HTML and CSS codes are the ones working

it's possible using openssh,when your PC wifi is connected to your phone hotspot
Re: A Thread For Competitive Programming by SUPREMOTM: 4:42pm On Feb 01, 2021
QUESTION 2

Language: JavaScript

IDE used: Bracket

Tested on Chrome browser console with the displayed results

Re: A Thread For Competitive Programming by logicDcoder(m): 6:34pm On Feb 01, 2021
Alert!
Next set of problems will be posted either tonight or tomorrow!
I really want to use this opportunity to thank SUPREMOTM , iAmChidiebere , 404Dev and logicDcoder for their modest contributions!

You Guys Are The Best!

1 Like

Re: A Thread For Competitive Programming by logicDcoder(m): 7:05pm On Feb 01, 2021
[Week 2 Questions]

1: In number theory, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For instance, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number. Write a function | isPerfectNumber( ) | that checks if a number is a perfect number or not return True if it's a perfect number and False if it's not.
https://en.wikipedia.org/wiki/Perfect_number

2: Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself. For example, the proper divisors of 6 are 1, 2, and 3.) A pair of amicable numbers constitutes an aliquot sequence of period 2. It is unknown if there are infinitely many pairs of amicable numbers.
Simple Example : (220,284) 220 is divisible by {1,2,4,5,10,11,20,22,44,55,110 } <- Sum = 284 and 284 is divisible by -> 1,2,4,71,142 and the Sum of that is. Yes right you probably expected it 220. Write a function (AmicableNumbers(int startValue, int StopValue) ) that finds all the amicable numbers in the range Start-value -> Stop-value and also time the function inother to know the time it took to find the pairs of numbers!

https://en.wikipedia.org/wiki/Amicable_numbers

3: Spice up Number 1 question by finding the Odd, Even and Prime perfect numbers in a custom range example: 1 -> 1000, 1000 -> 3000. What do you observe?
Honorable Mentions: SUPREMOTM iAmChidiebere 404Dev
GOOD LUCK!

2 Likes

(1) (2) (Reply)

Software Architecture / I Need To Learn Programming Seriously / Check Out My Recently Programmed Platform And Comment On It

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