Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,156,240 members, 7,829,436 topics. Date: Thursday, 16 May 2024 at 06:54 AM

Codewars.com Coding Challenges - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Codewars.com Coding Challenges (2385 Views)

Coding Challenges As A Newbie. / CODELAGOS: 337 Schools To Participate In Coding Competition / Do We Have Coding Bootcamps In Nigeria? / Faster Way To Learn Web Development. (2) (3) (4)

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

Re: Codewars.com Coding Challenges by Fr4nk(m): 7:45pm On May 19, 2022
excanny:
Got it working in C#
Nice
I haven't started doing it yet, I was busy with a tough codewars kata (write a function to check if a sudoku solution is valid or not)
I'll start now
Re: Codewars.com Coding Challenges by excanny: 8:06pm On May 19, 2022
Fr4nk:

Nice
I haven't started doing it yet, I was busy with a tough codewars kata (write a function to check if a sudoku solution is valid or not)
I'll start now

Can you share the problem?
Re: Codewars.com Coding Challenges by Fr4nk(m): 8:14pm On May 19, 2022
excanny:


Can you share the problem?

https://www.codewars.com/kata/53db96041f1a7d32dc0004d2

Write a function done_or_not/DoneOrNot passing a board (list[list_lines]) as parameter. If the board is valid return 'Finished!', otherwise return 'Try again!'

Sudoku rules:

Complete the Sudoku puzzle so that each and every row, column, and region contains the numbers one through nine only once.

Rows:


There are 9 rows in a traditional Sudoku puzzle. Every row must contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. There may not be any duplicate numbers in any row. In other words, there can not be any rows that are identical.

In the illustration the numbers 5, 3, 1, and 2 are the "givens". They can not be changed. The remaining numbers in black are the numbers that you fill in to complete the row.

Columns:


There are 9 columns in a traditional Sudoku puzzle. Like the Sudoku rule for rows, every column must also contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. Again, there may not be any duplicate numbers in any column. Each column will be unique as a result.

In the illustration the numbers 7, 2, and 6 are the "givens". They can not be changed. You fill in the remaining numbers as shown in black to complete the column.

Regions


A region is a 3x3 box like the one shown to the left. There are 9 regions in a traditional Sudoku puzzle.

Like the Sudoku requirements for rows and columns, every region must also contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. Duplicate numbers are not permitted in any region. Each region will differ from the other regions.

In the illustration the numbers 1, 2, and 8 are the "givens". They can not be changed. Fill in the remaining numbers as shown in black to complete the region.

Valid board example:


For those who don't know the game, here are some information about rules and how to play Sudoku: http://en.wikipedia.org/wiki/Sudoku and http://www.sudokuessentials.com/
Re: Codewars.com Coding Challenges by Fr4nk(m): 8:17pm On May 19, 2022
Fr4nk:


https://www.codewars.com/kata/53db96041f1a7d32dc0004d2

Write a function done_or_not/DoneOrNot passing a board (list[list_lines]) as parameter. If the board is valid return 'Finished!', otherwise return 'Try again!'

Sudoku rules:

Complete the Sudoku puzzle so that each and every row, column, and region contains the numbers one through nine only once.

Rows:


There are 9 rows in a traditional Sudoku puzzle. Every row must contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. There may not be any duplicate numbers in any row. In other words, there can not be any rows that are identical.

In the illustration the numbers 5, 3, 1, and 2 are the "givens". They can not be changed. The remaining numbers in black are the numbers that you fill in to complete the row.

Columns:


There are 9 columns in a traditional Sudoku puzzle. Like the Sudoku rule for rows, every column must also contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. Again, there may not be any duplicate numbers in any column. Each column will be unique as a result.

In the illustration the numbers 7, 2, and 6 are the "givens". They can not be changed. You fill in the remaining numbers as shown in black to complete the column.

Regions


A region is a 3x3 box like the one shown to the left. There are 9 regions in a traditional Sudoku puzzle.

Like the Sudoku requirements for rows and columns, every region must also contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. Duplicate numbers are not permitted in any region. Each region will differ from the other regions.

In the illustration the numbers 1, 2, and 8 are the "givens". They can not be changed. Fill in the remaining numbers as shown in black to complete the region.

Valid board example:


For those who don't know the game, here are some information about rules and how to play Sudoku: http://en.wikipedia.org/wiki/Sudoku and http://www.sudokuessentials.com/

The easy part was that I'm not new to sudoku and I solve a good number of puzzles for fun or pass time, I even remember building a sudoku solver using recursion and backtracking techniques (it was long ago, maybe i'll try it again but without the tutorial lol)
Re: Codewars.com Coding Challenges by Fr4nk(m): 11:20pm On May 19, 2022
Altairx440:
You're given s, a string with an arbitrary number of headers and rows in the formats below;

s1 = "Country\tState\tTown\r\nNigeria\tLagos
State\tLagos\r\nUSA\tTexas\tDallas\r\n"
s2 = "Country\r\nUSA\r\nChina;
s3 = "Name\tAge\r\n"

return an array of objects in the format below;
# s1
[
{
'Country': 'Nigeria',
'State': 'Lagos State',
'Town': 'Lagos'
},
{
'Country': 'USA',
'State': 'Texas',
'Town': 'Dallas'
}
]
# s2
[
{
'Country': USA'
},
{
'Country': 'China'
}
]
# s3
[]

Done, here's my solution in python


import json

s1 = "Country\tState\tTown\r\nNigeria\tLagos State\tLagos\r\nUSA\tTexas\tDallas\r\n"
s2 = "Country\r\nUSA\r\nChina"
s3 = "Name\tAge\r\n"

def hashify(s):
data_extract = [x.split("\t"wink for x in s.split("\r\n"wink]
keys = data_extract[0]
key_num = len(keys)
values = data_extract[1:]

if [''] in values:
values.pop(values.index(['']))

return [json.loads("{" + ", ".join(x) + "}"wink for x in [[f'"{keys[k]}":"{d[k]}"' for k in range(key_num)] for d in values]]


print(hashify(s1))
print(hashify(s2))
print(hashify(s3))

1 Like

Re: Codewars.com Coding Challenges by Fr4nk(m): 11:22pm On May 19, 2022
Fr4nk:


Done, here's my solution in python


import json

s1 = "Country\tState\tTown\r\nNigeria\tLagos State\tLagos\r\nUSA\tTexas\tDallas\r\n"
s2 = "Country\r\nUSA\r\nChina"
s3 = "Name\tAge\r\n"

def hashify(s):
data_extract = [x.split("\t"wink for x in s.split("\r\n"wink]
keys = data_extract[0]
key_num = len(keys)
values = data_extract[1:]

if [''] in values:
values.pop(values.index(['']))

return [json.loads("{" + ", ".join(x) + "}"wink for x in [[f'"{keys[k]}":"{d[k]}"' for k in range(key_num)] for d in values]]


print(hashify(s1))
print(hashify(s2))
print(hashify(s3))

replace all wink with ')'
Sorry for not adding comments smiley

1 Like

Re: Codewars.com Coding Challenges by Fr4nk(m): 11:27pm On May 19, 2022
I noticed that a line was too long to be viewed here,
So I'm. Sending a screenshot too

1 Like

Re: Codewars.com Coding Challenges by excanny: 12:28am On May 20, 2022
Great.
Re: Codewars.com Coding Challenges by excanny: 12:29am On May 20, 2022
You mind if we have a group with like minds. So we can always solve the challenges. Need people who are consistent and passionate for competitive programming

2 Likes

Re: Codewars.com Coding Challenges by Altairx440: 8:12am On May 20, 2022
Fr4nk:

Yes sir, python only, I may learn other languages like Javascript(soon because I want to work with flask or Django), R, C++ or Java or C# later, but being a python expert is d main goal
Cool, Of the languages you listed, it only R I haven't used before. You should really consider learning JavaScript bro, IMHO.
Re: Codewars.com Coding Challenges by Altairx440: 8:15am On May 20, 2022
excanny:
Got it working in C#
Nice one bro. We should be posting our code, that's the only way we'd be able test it for correctness. Also consider refactoring it into a function so it would be easier to test.
Re: Codewars.com Coding Challenges by Altairx440: 8:27am On May 20, 2022
Fr4nk:

Done, here's my solution in python
Works well, nice one.

1 Like

Re: Codewars.com Coding Challenges by Altairx440: 8:31am On May 20, 2022
Fr4nk:
I noticed that a line was too long to be viewed here,
So I'm. Sending a screenshot too
It's ok, I just have to click "Quote" to get the full code. BTW what editor/IDE is this?

1 Like

Re: Codewars.com Coding Challenges by Fr4nk(m): 8:49am On May 20, 2022
Altairx440:

It's ok, I just have to click "Quote" to get the full code. BTW what editor/IDE is this?
The one in the picture is just a text editor for android with syntax highlighting (it doesn't run code)
I'm actually using a web based editor called onecompiler.com I write all my codes and test them there, because I don't have a laptop now so I'm using my Android Tablet

1 Like

Re: Codewars.com Coding Challenges by Fr4nk(m): 8:53am On May 20, 2022
excanny:
You mind if we have a group with like minds. So we can always solve the challenges. Need people who are consistent and passionate for competitive programming

Yes sir, that would be great
Re: Codewars.com Coding Challenges by Altairx440: 9:16am On May 20, 2022
Fr4nk:

The one in the picture is just a text editor for android with syntax highlighting (it doesn't run code)
I'm actually using a web based editor called onecompiler.com I write all my codes and test them there, because I don't have a laptop now so I'm using my Android Tablet
Ok bro, I like the theme of the editor.
Re: Codewars.com Coding Challenges by Altairx440: 9:18am On May 20, 2022
excanny:
You mind if we have a group with like minds. So we can always solve the challenges. Need people who are consistent and passionate for competitive programming
What kind of group do you have in mind?
Re: Codewars.com Coding Challenges by excanny: 9:59am On May 20, 2022
Altairx440:

What kind of group do you have in mind?

WhatsApp is fine
Re: Codewars.com Coding Challenges by excanny: 10:11am On May 20, 2022
Fr4nk:


https://www.codewars.com/kata/53db96041f1a7d32dc0004d2

Write a function done_or_not/DoneOrNot passing a board (list[list_lines]) as parameter. If the board is valid return 'Finished!', otherwise return 'Try again!'

Sudoku rules:

Complete the Sudoku puzzle so that each and every row, column, and region contains the numbers one through nine only once.

Rows:


There are 9 rows in a traditional Sudoku puzzle. Every row must contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. There may not be any duplicate numbers in any row. In other words, there can not be any rows that are identical.

In the illustration the numbers 5, 3, 1, and 2 are the "givens". They can not be changed. The remaining numbers in black are the numbers that you fill in to complete the row.

Columns:


There are 9 columns in a traditional Sudoku puzzle. Like the Sudoku rule for rows, every column must also contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. Again, there may not be any duplicate numbers in any column. Each column will be unique as a result.

In the illustration the numbers 7, 2, and 6 are the "givens". They can not be changed. You fill in the remaining numbers as shown in black to complete the column.

Regions


A region is a 3x3 box like the one shown to the left. There are 9 regions in a traditional Sudoku puzzle.

Like the Sudoku requirements for rows and columns, every region must also contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. Duplicate numbers are not permitted in any region. Each region will differ from the other regions.

In the illustration the numbers 1, 2, and 8 are the "givens". They can not be changed. Fill in the remaining numbers as shown in black to complete the region.

Valid board example:


For those who don't know the game, here are some information about rules and how to play Sudoku: http://en.wikipedia.org/wiki/Sudoku and http://www.sudokuessentials.com/

You have to loop through the 2D array. You have to check that the rows and columns don’t contain duplicate values. A hashset can do that job. You also check that the values are within 0-9. Finally you check that the 3 x 3 matrices don’t contain duplicates.

1 Like

Re: Codewars.com Coding Challenges by Altairx440: 10:17am On May 20, 2022
excanny:


WhatsApp is fine
I do think we're too few to form an active group, plus getting new members would be another thing to consider.
Re: Codewars.com Coding Challenges by Fr4nk(m): 10:30am On May 20, 2022
excanny:


You have to loop through the 2D array. You have to check that the rows and columns don’t contain duplicate values. A hashset can do that job. You also check that the values are within 0-9. Finally you check that the 3 x 3 matrices don’t contain duplicates.

Yeah on point, the hardest part for me was validating each of the 9 3x3 regions,
Here's what I did tho
I found a way to convert the 3x3 region into a 1D array and validate it just like you did for the rows and columns
Re: Codewars.com Coding Challenges by Altairx440: 12:41pm On May 20, 2022
Fr4nk:


https://www.codewars.com/kata/53db96041f1a7d32dc0004d2

Write a function done_or_not/DoneOrNot passing a board (list[list_lines]) as parameter. If the board is valid return 'Finished!', otherwise return 'Try again!'
# My solution:

def validate(arr):
if sum(arr) != 45:
return False
return True

def paginate(arr):
return [arr[x:x+3] for x in range(0,9,3)]

def done_or_not(board):
for row in board:
if not validate(row):
return "Try again!"
for i in range(len(board[0])):
column = []
for j in range(len(board)):
column.append(board[j][i])
if not validate(column):
return "Try again!"

regions = [[]] * len(board)
for i in range(3):
tmp = paginate(board[i])
tmp += paginate(board[i+3])
tmp += paginate(board[i+6])
for k in range(len(board)):
regions[k] = regions[k] + tmp[k]
for region in regions:
if not validate(region):
return "Try again!"
return "Finished!"
Re: Codewars.com Coding Challenges by Fr4nk(m): 7:16pm On May 20, 2022
Altairx440:

# My solution:

def validate(arr):
if sum(arr) != 45:
return False
return True

def paginate(arr):
return [arr[x:x+3] for x in range(0,9,3)]

def done_or_not(board):
for row in board:
if not validate(row):
return "Try again!"
for i in range(len(board[0])):
column = []
for j in range(len(board)):
column.append(board[j][i])
if not validate(column):
return "Try again!"

regions = [[]] * len(board)
for i in range(3):
tmp = paginate(board[i])
tmp += paginate(board[i+3])
tmp += paginate(board[i+6])
for k in range(len(board)):
regions[k] = regions[k] + tmp[k]
for region in regions:
if not validate(region):
return "Try again!"
return "Finished!"

It works. Great job.
But please try dey comment your code boss grin
It's the same approach as mine but with a different implementation, which is actually cool

1 Like

Re: Codewars.com Coding Challenges by Houstency(m): 7:17pm On May 20, 2022
Just started learning Python this week via HNI internship and a total newbie to programming. Kudos guys, I like what you guys are doing, should be able to join you guys as I progress lol. Tips, tricks etc to aid my learning would be highly welcomed.

1 Like

Re: Codewars.com Coding Challenges by Fr4nk(m): 7:21pm On May 20, 2022
My solution.py

def done_or_not(board): #board[i][j]
# check horizontals
#check if there are duplicates in the array
for i in board:
if len(set(i)) != 9:
return 'Try again!'

# check verticals
#check if there are duplicates in the array
for j in range(9):
if len(set(x[j] for x in board)) != 9:
return 'Try again!'

#check regions
#convert 3x3 region into a single one-dimensional array
#check if there are duplicates in the array
#yeah this was the really hard part for me LOL

c = 0
d = 3
for n in range(3):
a = 0
b = 3
for m in range(3):
region = []
for k in range(c,d):
for l in ([x[k] for x in board][a:b]):
region.append(l)

if len(set(region)) != 9:
return "Try again!"

a += 3
b += 3
c += 3
d += 3


return 'Finished!'


if __name__ == '__main__':
print(done_or_not([[1, 2, 3, 4, 5, 6, 7, 8, 9],
[2, 3, 4, 5, 6, 7, 8, 9, 1],
[3, 4, 5, 6, 7, 8, 9, 1, 2],
[4, 5, 6, 7, 8, 9, 1, 2, 3],
[5, 6, 7, 8, 9, 1, 2, 3, 4],
[6, 7, 8, 9, 1, 2, 3, 4, 5],
[7, 8, 9, 1, 2, 3, 4, 5, 6],
[8, 9, 1, 2, 3, 4, 5, 6, 7],
[9, 1, 2, 3, 4, 5, 6, 7, 8]]))

1 Like

Re: Codewars.com Coding Challenges by Fr4nk(m): 7:23pm On May 20, 2022
It's the nested
for
loops for me grin

1 Like

Re: Codewars.com Coding Challenges by Fr4nk(m): 7:25pm On May 20, 2022
Houstency:
Just started learning Python this week via HNI internship and a total newbie to programming. Kudos guys, I like what you guys are doing, should be able to join you guys as I progress lol. Tips, tricks etc to aid my learning would be highly welcomed.
Thank you and welcome to the club boss, we can't wait to see your codes
Re: Codewars.com Coding Challenges by Altairx440: 10:05pm On May 20, 2022
Fr4nk:

It works. Great job.
But please try dey comment your code boss grin
It's the same approach as mine but with a different implementation, which is actually cool
Yeah, I don't really fancy comments lol, that's why I use mostly descriptive function and variables name, I only use comments when it's absolutely necessary.
Re: Codewars.com Coding Challenges by Altairx440: 10:09pm On May 20, 2022
Fr4nk:
It's the nested
for
loops for me grin
Four nested loops is no joke lmao.
Re: Codewars.com Coding Challenges by Temmylee01(m): 9:13am On May 29, 2022
I'm getting my laptop tomorrow morning, nd I should be starting to learn programming this week. My prob now is who to put me through since I'm in abj.

1 Like

Re: Codewars.com Coding Challenges by Fr4nk(m): 4:40pm On May 29, 2022
Temmylee01:
I'm getting my laptop tomorrow morning, nd I should be starting to learn programming this week. My prob now is who to put me through since I'm in abj.

Cool bro, but your location doesn't matter, I learnt recursion from a professor in Germany right from my home here in Lagos, learn to make the internet your greatest weapon as a programmer, its not cheating even if you ask google for something as simple as "how do I terminate a for loop", forget that BS that people say about relying too much on Google for your answers, You need to Google everything if you have to because nobody in abuja or anywhere else will be able to answer all your questions,

Watch tutorials on YouTube, and read programming blogs you'll be okay
Re: Codewars.com Coding Challenges by Temmylee01(m): 7:12am On May 30, 2022
Fr4nk:


Cool bro, but your location doesn't matter, I learnt recursion from a professor in Germany right from my home here in Lagos, learn to make the internet your greatest weapon as a programmer, its not cheating even if you ask google for something as simple as "how do I terminate a for loop", forget that BS that people say about relying too much on Google for your answers, You need to Google everything if you have to because nobody in abuja or anywhere else will be able to answer all your questions,

Watch tutorials on YouTube, and read programming blogs you'll be okay
I agree with you, but those YouTube class are somehow complicated reason is they hardly explain very well. The guy will just be talking and talking.

1 Like 1 Share

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

Should You Study Computer Science To Learn How To Code? / C++ Question / Developer Integrates Interswitch's Webpay With Wordpress Plugin

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