Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,149,866 members, 7,806,460 topics. Date: Tuesday, 23 April 2024 at 04:51 PM

Competitive Programming - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Competitive Programming (2490 Views)

A Thread For Competitive Programming / Thread For Competitive Programming (2) (3) (4)

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

Re: Competitive Programming by Nobody: 10:48am On Jul 30, 2020
vheckthor1:


function maxer(arr){
let holder = 0;
for(let i=0; i< arr.length; i++){
let some = arr.slice(arr[i])
let maxima = Math.max(...some)
let subractor=maxima - arr[i]
if(subractor>holder){
holder = subractor
}
}
return holder
}
solved in javascript and the big O(n)=1
a single loop
I think this is O(n^2) because of the max function. I'll try and explain what I mean later.
Re: Competitive Programming by Patiee: 10:54am On Jul 30, 2020
Excuse me, I'm out of the topic. Does anybody here know how long it takes for the openWeather API keys to be activated. They emailed me that it will take a couple of hours and it has been a day now.
Re: Competitive Programming by Donpre(m): 1:04pm On Jul 31, 2020
Darivie04:

Must be nairaland formatting cutting out part of the code, I'm still tryna fix that. It returns 5 over here.

Hello, what book/resource did you use to study algorithms
Re: Competitive Programming by Brukx(m): 12:14am On Aug 01, 2020
Karleb:
How about this one?

array=[[1,2,3,4,5],[6,7,8,9,10,],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25]]

clockwiseList=[]
mainlist=[]
def clockwise(array):
newlist=[]
for ch in array[0]:
newlist.append(ch)
array.pop(0)
for ch in array:
newlist.append(ch[-1])
ch.pop(-1)
array[-1].reverse()
for ch in array[-1]:
newlist.append(ch)
array.pop(-1)
newRow=[]
for ch in array:
newRow.append(ch[0])
ch.pop(0)
newRow.reverse()
newlist.extend(newRow)
mainlist.append(newlist)
if len(array)>1:
clockwise(array)
else:
for ch in array:
mainlist.append(ch)
for ch in mainlist:
for chr in ch:
clockwiseList.append(chr)
print("\nthis is the Clockwise array\n"+str(clockwiseList))
clockwise(array)


I think this will do

1 Like

Re: Competitive Programming by Donpre(m): 12:50am On Aug 01, 2020
Brukx:


array=[[1,2,3,4,5],[6,7,8,9,10,],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25]]

clockwiseList=[]
mainlist=[]
def clockwise(array):
newlist=[]
for ch in array[0]:
newlist.append(ch)
array.pop(0)
for ch in array:
newlist.append(ch[-1])
ch.pop(-1)
array[-1].reverse()
for ch in array[-1]:
newlist.append(ch)
array.pop(-1)
newRow=[]
for ch in array:
newRow.append(ch[0])
ch.pop(0)
newRow.reverse()
newlist.extend(newRow)
mainlist.append(newlist)
if len(array)>1:
clockwise(array)
else:
for ch in array:
mainlist.append(ch)
for ch in mainlist:
for chr in ch:
clockwiseList.append(chr)
print("\nthis is the Clockwise array\n"+str(clockwiseList))
clockwise(array)


I think this will do
Wow! brilliant approach but I got lost at the if-else statement. What does that do?
Re: Competitive Programming by Brukx(m): 1:12am On Aug 01, 2020
Donpre:

Wow! brilliant approach but I got lost at the if-else statement. What does that do?

I've replied you on WhatsApp. But in case for others that might find it helpful

The first for-loop will append the reminants of the array into the mainlist. But at times the main list is 2 dimensional.
So the second for-loop iterates through all the elements of the mainlist and copies them into the clockwiseList thus final list(clockwiseList) is one dimensional
Re: Competitive Programming by LucaB: 10:40am On Aug 03, 2020
Brukx:


array=[[1,2,3,4,5],[6,7,8,9,10,],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25]]

clockwiseList=[]
mainlist=[]
def clockwise(array):
newlist=[]
for ch in array[0]:
newlist.append(ch)
array.pop(0)
for ch in array:
newlist.append(ch[-1])
ch.pop(-1)
array[-1].reverse()
for ch in array[-1]:
newlist.append(ch)
array.pop(-1)
newRow=[]
for ch in array:
newRow.append(ch[0])
ch.pop(0)
newRow.reverse()
newlist.extend(newRow)
mainlist.append(newlist)
if len(array)>1:
clockwise(array)
else:
for ch in array:
mainlist.append(ch)
for ch in mainlist:
for chr in ch:
clockwiseList.append(chr)
print("\nthis is the Clockwise array\n"+str(clockwiseList))
clockwise(array)


I think this will do

Nice approach
Re: Competitive Programming by LucaB: 10:16pm On Aug 03, 2020
What materials do you guys make use of for data structures?
Re: Competitive Programming by Karleb(m): 10:15am On Aug 04, 2020
LucaB:
What materials do you guys make use of for data structures?

Google
Re: Competitive Programming by vheckthor1: 8:39pm On Aug 04, 2020
I uploaded answers it was deleted, I was even ban from making comments in programming section for a while. what have I done wrong?
Re: Competitive Programming by Jummate(m): 10:51pm On Aug 04, 2020
Karleb:
How about this one?

def spiralPrinting(anArr): rootList = [] def inner(arr): rootList.extend(arr[0]) arr.pop(0) for each in arr: rootList.append(each.pop()) arr[-1].reverse() rootList.extend(arr.pop()) if len(arr) > 0: for x in range(len(arr) -1, -1, -1): rootList.append(arr[x].pop(0)) if len(arr) > 1: inner(arr) else: if len(arr) > 0: rootList.extend(arr.pop())
inner(anArr) return rootList if __name__ == '__main__': multi = [[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], [16,17,18,19,20]] print(spiralPrinting(multi))






This will work for any size of this problem
Re: Competitive Programming by ToyinDipo(m): 2:08am On Aug 05, 2020
These clockwise algorithms posted so far will execute in O(n ^ 2) where (r * c = n), due to the reverse method calls.
I am not sure it needs to be complicated, I wish I could explain without writing the code

Didn't test nor write in an IDE, but this should prolly work


input = int[x][y]
output = int[x][y]

boolean[][] visited = new boolean[i][j];
int n, i, j = 0; boolean right = true;

while (n < (x * y)) {
output[n/j][n%j] = input[i][j];
++n;
visited[i][j] = true;

if (n >= (x * y)) return;
if (right) {
++j;
if (j == y || visited[i][j]) {
--j;
++i;
if (i == x || visited[i][j]) {
right = false;
++j;
--i;
}
}
} else {
--j;
if (j == -1 || visited[i][j]) {
++j;
--i;
if (i == -1 || visited[i][j]) {
right = true;
--j;
++i;
}
}
}
}
Re: Competitive Programming by Karleb(m): 2:55am On Aug 05, 2020
.

Re: Competitive Programming by Karleb(m): 2:58am On Aug 05, 2020
....

Re: Competitive Programming by Brukx(m): 7:46am On Aug 05, 2020
Karleb:
.
This na heavy challenge o
Re: Competitive Programming by Karleb(m): 8:51am On Aug 05, 2020
Brukx:
This na heavy challenge o

grin

I've actually been looking at it for a while and I don't want to conclude it a tree.

But why don't you Google it, there is no sin in that.


Or you could attempt the second question which is quite simple. Just implement the findMin function, it'll get the deepest value which in this case is on the left side of the BST.
Re: Competitive Programming by Brukx(m): 9:26am On Aug 05, 2020
Karleb:


grin

I've actually been looking at it for a while and I don't want to conclude it a tree.

But why don't you Google it, there is no sin in that.


Or you could attempt the second question which is quite simple. Just implement the findMin function, it'll get the deepest value which in this case is on the left side of the BST.
Google Kwa?
I'm almost done with it though

1 Like

Re: Competitive Programming by Nobody: 9:30am On Aug 05, 2020
Deicide:
Never knew Nigerians were interested in Competitive programming.
Me too.... i was shocked to this post. A normal naija IT person just learn python for 3 months jump Algorithms and Data structures + their implementations, and the learn django or jump into Data Sci.,Ai etc.

1 Like

Re: Competitive Programming by Nobody: 9:33am On Aug 05, 2020
tensazangetsu20:

The question looks weird. Why 5 and not 6. 11-5. Isn't the goal of selling stocks to get profit. Why not gain more profit. If I had this question the first thing I will do is to sort the array. Then I would loop through the array twice for every loop I will subtract the preceding number from the oncoming number and place that in a constant and return the highest number. The only problem with loops is the time complexity o(n).

use recursion instead of iteration.
Re: Competitive Programming by Nobody: 9:36am On Aug 05, 2020
pseudonomer:
Lately, I’ve been involved in lots programming interviews which involves a lot of data structures and algorithm questions. I discovered practice and practice and practice is all I need. I’m using this thread to challenge myself to solve at least two problems per day. Others can join me too.

Key to Success: Strong Background in Data Structure and Algorithm, and Logic Mathematics.

Languages: C++, and sometimes Java. Others can use Python.

bro you can actually help yourself by registering with codesignal. search "codesignal" using google-Register and start practicing.
Re: Competitive Programming by Nobody: 9:38am On Aug 05, 2020
Karleb:
For those that love the challenge.
This is a typical python problem
Re: Competitive Programming by Nobody: 9:49am On Aug 05, 2020
Looking at the solutions you guys paste here... most of it were all copied.
Re: Competitive Programming by Karleb(m): 9:57am On Aug 05, 2020
BlaqTesla:
Looking at the solutions you guys paste here... most of it were all copied.

It's okay to copy answer.


No one is grading anyone here, most important is the solution and explanation if you could.
Re: Competitive Programming by Karleb(m): 10:00am On Aug 05, 2020
BlaqTesla:

This is a typical python problem

I'll be happy to see your solution
Re: Competitive Programming by Nobody: 10:03am On Aug 05, 2020
Karleb:


I'll be happy to see your solution
I don't write python. I code on C language.
Re: Competitive Programming by Karleb(m): 10:06am On Aug 05, 2020
BlaqTesla:

I don't write python. I code on C language.

Give your answer in C then.
Re: Competitive Programming by Nobody: 10:12am On Aug 05, 2020
Karleb:


Give your answer in C then.
we don't do turple in C.
Re: Competitive Programming by Donpre(m): 10:16am On Aug 05, 2020
BlaqTesla:
Looking at the solutions you guys paste here... most of it were all copied.
Are you saying Nigerians aren't smart enough to come up with answers to those questions?

1 Like

Re: Competitive Programming by Brukx(m): 10:49am On Aug 05, 2020
Donpre:

Are you saying Nigerians aren't smart enough to come up with answers to those questions?
Some persons are good at doubting what they don't understand

1 Like

Re: Competitive Programming by Nobody: 10:57am On Aug 05, 2020
Karleb:
.
I think I've solved this.
Re: Competitive Programming by Donpre(m): 11:05am On Aug 05, 2020
Brukx:
Some persons are good at doubting what they don't understand
Ah swear, it's annoying. Like what's so hard there that one of us can't solve.

The belief that Nigerians can't compete with the rest of the world is just sick. We're not there yet but we're not dumb either

1 Like

(1) (2) (3) (Reply)

Working On Creating A Test Database. Anyone With Sample Data For Free / I Need Help With Android Studio! / Developers, Come In Lets Be Truthful.

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