Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,483 members, 7,816,143 topics. Date: Friday, 03 May 2024 at 06:28 AM

Play With Data Structures And Algorithm (all Languages) - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Play With Data Structures And Algorithm (all Languages) (1295 Views)

What Programming Language Is Best To Learn Data Structures And Algorithms? / Accountability Partner To Study Data Structures And Algorithm With. / How Important Is Data Structures And Algorithm To Pro Developers (2) (3) (4)

(1) (Reply) (Go Down)

Play With Data Structures And Algorithm (all Languages) by daddynasa: 11:15am On Mar 14, 2022
I will be dropping some DSA questions for relaxation.

Rule 1
Please drop your pseudo-code and solution

Rule 2
Include time and space complexity

Rule 3
All devs are allowed to review and argue constructively on different solutions.

Rule 4
Working code is always better than wrong code as optimization would be suggested by other devs.

Rule 5
Only for interested devs

We will be progressing from easy to medium to difficult

LABS will be dropped twice a week.

1 Like

Re: Play With Data Structures And Algorithm (all Languages) by daddynasa: 11:15am On Mar 14, 2022
WEEK1

LAB 1.

Given a non-empty string like "Code" return a string like "CCoCodCode".

stringSplosion("Code"wink → "CCoCodCode"
stringSplosion("abc"wink → "aababc"
stringSplosion("ab"wink → "aab"

function (str) {

return str;
}


LAB 2.

A team of developers, have a project to build a new programming language,
they need a developer to build them a syntax reader for braces only to check if braces are properly closed
this determines if the syntax is correct or not.
your task is to build a syntax reader to check if syntax is correct or return the position of the error brace

given a string of syntax of length N containing only braces "[]{}()<>",
check that the syntax is correct or return the position of the wrong braces
if correct return "ok", if wrong, return the position of the first error.

constraint:
0 <= N <= 100000000
N contain only these characters {}[]()<>

For example:
readSyntax('{{}}') => 'ok'
readSyntax('[[>]') => 2
readSyntax('[[>]{}()<>') => 2

ENJOY!!!!!!!!

1 Like

Re: Play With Data Structures And Algorithm (all Languages) by Altairx440: 7:37pm On Mar 14, 2022
daddynasa:
LAB 1.

Given a non-empty string like "Code" return a string like "CCoCodCode".

stringSplosion("Code"wink → "CCoCodCode"
stringSplosion("abc"wink → "aababc"
stringSplosion("ab"wink → "aab"

function (str) {

return str;
}
python

def foo(s):
----r = ""
----for i in range(len(s)+1):
--------r += s[:i]
----return r

2 Likes

Re: Play With Data Structures And Algorithm (all Languages) by daddynasa: 8:51pm On Mar 14, 2022
Altairx440:

python

def foo(s):
----r = ""
----for i in range(len(s)+1):
--------r += s[:i]
----return r

This is O(n) time and O(n) space
Re: Play With Data Structures And Algorithm (all Languages) by Altairx440: 10:56pm On Mar 14, 2022
daddynasa:

This is O(n) time and O(n) space
Oh my i didn't even include that, thanks.
Re: Play With Data Structures And Algorithm (all Languages) by DDayve: 11:26pm On Mar 14, 2022
//JavaScript
function stringSplosion(str){
let result = '';
for (let i=0; i<str.length; i++){
result += str.slice(0, i+1)
}
return result
}

//Time: O(n) , Space: O(n)

2 Likes

Re: Play With Data Structures And Algorithm (all Languages) by Kvngfrosh(m): 12:41am On Mar 15, 2022
DDayve:
//JavaScript
function stringSplosion(str){
let result = '';
for (let i=0; i<str.length; i++){
result += str.slice(0, i+1)
}
return result
}

//Time: O(n) , Space: O(n)
Nice

1 Like

Re: Play With Data Structures And Algorithm (all Languages) by daddynasa: 7:44am On Mar 15, 2022
I have added the second lab for the week
Re: Play With Data Structures And Algorithm (all Languages) by Frontend: 4:23pm On Mar 15, 2022
DDayve:
//JavaScript
function stringSplosion(str){
let result = '';
for (let i=0; i<str.length; i++){
result += str.slice(0, i+1)
}
return result
}

//Time: O(n) , Space: O(n)

This is for loop
Re: Play With Data Structures And Algorithm (all Languages) by Nobody: 5:23pm On Mar 15, 2022
Following!
Re: Play With Data Structures And Algorithm (all Languages) by DDayve: 9:19pm On Mar 15, 2022
This is not the best solution but lemme drop it. i would take correction from my seniors here. Though it passes the 3 test you provided for it with Time Complexity of O(n) and space complexity of O(1).
//Javascript
function readSyntax(str){
let blk =0;
let curl =0;
let circ = 0;
let grt = 0
function checking(char){
switch (char) {
case '[':
blk +=1 ;
break;
case ']':
blk -=1 ;
break;

case '{':
curl +=1 ;
break;
case '}':
curl -=1 ;
break;

case '(':
circ +=1 ;
break;
case ')':
circ -=1 ;
break;

case '<':
grt +=1 ;
break;
case '>':
grt -=1 ;
break;

default:
break;
}
}
for (let i=0; i<str.length; i++){
checking(str[i])
}

if (blk===0 && curl===0 && circ===0 && grt===0){
return 'Ok'
} else if (blk !=0){
return str.lastIndexOf('[') + 1
} else if (curl !=0){
return str.lastIndexOf('{') + 1
} else if (circ !=0){
return str.lastIndexOf('(') + 1
} else{
return str.lastIndexOf('<') + 1
}
}


daddynasa:
WEEK1

LAB 2.

A team of developers, have a project to build a new programming language,
they need a developer to build them a syntax reader for braces only to check if braces are properly closed
this determines if the syntax is correct or not.
your task is to build a syntax reader to check if syntax is correct or return the position of the error brace

given a string of syntax of length N containing only braces "[]{}()<>",
check that the syntax is correct or return the position of the wrong braces
if correct return "ok", if wrong, return the position of the first error.

constraint:
0 <= N <= 100000000
N contain only these characters {}[]()<>

For example:
readSyntax('{{}}') => 'ok'
readSyntax('[[>]') => 2
readSyntax('[[>]{}()<>') => 2

ENJOY!!!!!!!!
Re: Play With Data Structures And Algorithm (all Languages) by Altairx440: 9:37pm On Mar 15, 2022
daddynasa:
WEEK1
LAB 2

readSyntax('{{}}') => 'ok'
readSyntax('[[>]') => 2
readSyntax('[[>]{}()<>') => 2

ENJOY!!!!!!!!

# python
# time: O(n)
# space: O(n/2)

def read_syntax(s):
~~stack = []
~~for i in range(len(s)):
~~~~ch = s[i]
~~~~if ch in '<{[(':
~~~~~~stack.append(ch)
~~~~else:
~~~~~~if len(stack) == 0:
~~~~~~~~return i
~~~~~~ch2 = stack.pop()
~~~~~~if not ((ch2 == '{' and ch == '}') or
~~~~~~~~~(ch2 == '<' and ch == '>') or
~~~~~~~~~(ch2 == '(' and ch == ')') or
~~~~~~~~~(ch2 == '[' and ch == ']')):
~~return i
~~if len(stack) == 0:
~~~~return 'ok'
~~else:
~~~~return 0

1 Like

Re: Play With Data Structures And Algorithm (all Languages) by Altairx440: 9:57pm On Mar 15, 2022
@daddynasa

LAB 2 is not well written, specifically, languages with static typing do not allow the return of more than one type.
Re: Play With Data Structures And Algorithm (all Languages) by daddynasa: 10:08am On Mar 16, 2022
Altairx440:
@daddynasa

LAB 2 is not well written, specifically, languages with static typing do not allow the return of more than one type.

I get you. May a return of -1 for “ok”
Although I have not ran your solution but
Using a stack data structure is a key to an easy solution
Well done.

Space complexity for your solution is O(n) constants are ignored.
Re: Play With Data Structures And Algorithm (all Languages) by DDayve: 11:56am On Mar 16, 2022
No comment on mine? Also, why are people not participating? Would have loved to see different solutions so I can learn different ways of doing things.
daddynasa:


I get you. May a return of -1 for “ok”
Although I have not ran your solution but
Using a stack data structure is a key to an easy solution
Well done.

Space complexity for your solution is O(n) constants are ignored.
Re: Play With Data Structures And Algorithm (all Languages) by Altairx440: 12:23pm On Mar 16, 2022
daddynasa:


I get you. May a return of -1 for “ok”
Although I have not ran your solution but
Using a stack data structure is a key to an easy solution
Well done.

Space complexity for your solution is O(n) constants are ignored.

Yeah, true.
It's such a pain to indent with "~" or "-", though IMO is more readable than zero indents.
Re: Play With Data Structures And Algorithm (all Languages) by Altairx440: 12:38pm On Mar 16, 2022
DDayve:
No comment on mine? Also, why are people not participating? Would have loved to see different solutions so I can learn different ways of doing things.
Most of these people have zero interest for the process, they just want the money.

2 Likes

Re: Play With Data Structures And Algorithm (all Languages) by Kvngfrosh(m): 3:13pm On Mar 16, 2022
Omo if I know Dsa I would have participated, na only the first task I understand
Re: Play With Data Structures And Algorithm (all Languages) by daddynasa: 4:40pm On Mar 16, 2022
DDayve:
No comment on mine? Also, why are people not participating? Would have loved to see different solutions so I can learn different ways of doing things.

Apologies. I will go through yours and other devs can comment too.

The intention was to get most devs to participate so that everyone can learn how things are done differently and why they chose the approach. But most NL devs na copy and paste or those kind of “SELECT * FROM users” developers. grin grin grin grin NA JOKE O

Anyways let us be patient as more devs will see the post
Re: Play With Data Structures And Algorithm (all Languages) by daddynasa: 5:19pm On Mar 16, 2022
DDayve:
This is not the best solution but lemme drop it. i would take correction from my seniors here. Though it passes the 3 test you provided for it with Time Complexity of O(n) and space complexity of O(1).
//Javascript
function readSyntax(str){
let blk =0;
let curl =0;
let circ = 0;
let grt = 0
function checking(char){
switch (char) {
case '[':
blk +=1 ;
break;
case ']':
blk -=1 ;
break;

case '{':
curl +=1 ;
break;
case '}':
curl -=1 ;
break;

case '(':
circ +=1 ;
break;
case ')':
circ -=1 ;
break;

case '<':
grt +=1 ;
break;
case '>':
grt -=1 ;
break;

default:
break;
}
}
for (let i=0; i<str.length; i++){
checking(str[i])
}

if (blk===0 && curl===0 && circ===0 && grt===0){
return 'Ok'
} else if (blk !=0){
return str.lastIndexOf('[') + 1
} else if (curl !=0){
return str.lastIndexOf('{') + 1
} else if (circ !=0){
return str.lastIndexOf('(') + 1
} else{
return str.lastIndexOf('<') + 1
}
}

Your code takes 7+ seconds to run and only passed the three test cases. After running your code against all possible cases,
From your code, what happens in the following cases?
1. readSyntax("{<[("wink => 0
2. readSyntax("{"wink => 0
Re: Play With Data Structures And Algorithm (all Languages) by DDayve: 8:28pm On Mar 16, 2022
Thanks. I would use some conditionals to correct that. The code does not run in 7secs rather it runs in 7milliSeconds, which is 0.007sec

daddynasa:

Your code takes 7+ seconds to run and only passed the three test cases. After running your code against all possible cases,
From your code, what happens in the following cases?
1. readSyntax("{<[("wink => 0
2. readSyntax("{"wink => 0


Re: Play With Data Structures And Algorithm (all Languages) by 3exe3: 1:04am On Mar 19, 2022
function slice(array $data, int $start, int $length, array $result = array ()):array
{

if($start == $length -1)
{
return $result;
}
$result[] = $data[$start];
slice($data, $start +=1, $length, $result);
}

function copy (string $casestudy): string
{
$length = 0;
$result = null;
$newcasestudy = str_split($casestudy);
for($i = 1; $i < count($newcasestudy); $i++)
{
$result = slice($casestudy, 0, $i);
}
return implode(',', $result);
}



Here are my codes for lab1


Still practicing recursion

Not a guru
Re: Play With Data Structures And Algorithm (all Languages) by LucaB: 8:57am On Mar 30, 2022
Please can you explain str.slice(0,i+1)

DDayve:
//JavaScript
function stringSplosion(str){
let result = '';
for (let i=0; i<str.length; i++){
result += str.slice(0, i+1)
}
return result
}

//Time: O(n) , Space: O(n)

(1) (Reply)

Jesus!!!! No Nigerian Dev Can Build This Type Of Website / Are You a Geek? [The New Geeks] / I Need Help On Programming

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