Outline ·
[ Standard ] ·
Linear+
Frontend Developers, what's happening?
TSsilverhawk
|
Jul 7 2022, 10:44 PM
|
I'm Positively Lustrous
|
QUOTE(nyem @ Jul 7 2022, 10:03 PM) Not sure what you mean by dynamic, but here's my version of a table with selectable and sortable columnsCame across this javascript interview questions and I fail every single one of them  dynamic meaning the data changes, so you're really creating a reusable component that automatically creates the headers. Try loading this endpoint: https://sandbox.darylphuah.com/invq1/data.php Each time you load the endpoint, it should return you a randomly chosen dataset. Your component should be able to render them all. There are a few little tricks in some datasets.
|
|
|
|
FlierMate1
|
Jul 7 2022, 11:29 PM
|
Getting Started

|
QUOTE(silverhawk @ Jul 7 2022, 10:44 PM) "darylphuah".... OMG, now I understand you better.
|
|
|
|
nyem
|
Jul 8 2022, 12:04 AM
|
|
QUOTE(silverhawk @ Jul 7 2022, 10:44 PM) dynamic meaning the data changes, so you're really creating a reusable component that automatically creates the headers. Try loading this endpoint: https://sandbox.darylphuah.com/invq1/data.php Each time you load the endpoint, it should return you a randomly chosen dataset. Your component should be able to render them all. There are a few little tricks in some datasets. CORS prevented access to that endpoint. Otherwise can test from https://bl.ocks.org/nyem69/29896289104af5b5a5e27a61041756a9CORS proxy : https://bl.ocks.org/nyem69/c674d91ac78ea44723bf6ad26fc3fb6cThis post has been edited by nyem: Jul 8 2022, 03:39 AM
|
|
|
|
TSsilverhawk
|
Jul 8 2022, 12:15 AM
|
I'm Positively Lustrous
|
QUOTE(FlierMate1 @ Jul 7 2022, 11:29 PM) "darylphuah".... OMG, now I understand you better. 
|
|
|
|
15cm
|
Jul 8 2022, 07:08 PM
|
Getting Started

|
QUOTE(FlierMate1 @ Jul 7 2022, 11:29 PM) "darylphuah".... OMG, now I understand you better.   *googles furiously*
|
|
|
|
TSsilverhawk
|
Jul 8 2022, 07:16 PM
|
I'm Positively Lustrous
|
QUOTE(15cm @ Jul 8 2022, 07:08 PM)  *googles furiously*  My identity is quite public actually
|
|
|
|
FlierMate1
|
Jul 8 2022, 07:30 PM
|
Getting Started

|
QUOTE(15cm @ Jul 8 2022, 07:08 PM)  *googles furiously* QUOTE(silverhawk @ Jul 8 2022, 07:16 PM)  My identity is quite public actually Haha. Can I say so, one of the best web developer partnered with the one of the best web designer. This post has been edited by FlierMate1: Jul 9 2022, 12:00 AM
|
|
|
|
Fraus
|
Nov 16 2022, 02:05 AM
|
New Member
|
Can I ask in behalf of TS?  Another challenge, similar to original question posed by silverhawk, but this time print a tree (or triangle) with variable size.
https://pictr.com/images/2022/11/16/EadV9c.png
Of course, you can simply print each line, but what if now you must solve it using for...loop (with variable size input from user).
|
|
|
|
angch
|
Nov 16 2022, 11:54 AM
|
|
CODE % echo 10 | perl -e'$_=<>;--$_;print $_=" "x$_."*"." "x$_."\n";print$_ while(s/ \*/\*\*/g&&s/\* /\*\*/g);' * *** ***** ******* ********* *********** ************* *************** ***************** *******************
 No for loop, failed. It's just a 1D cellular automata. This post has been edited by angch: Nov 16 2022, 12:01 PM
|
|
|
|
Fraus
|
Nov 16 2022, 03:06 PM
|
New Member
|
QUOTE(angch @ Nov 16 2022, 11:54 AM)
CODE % echo 10 | perl -e'$_=<>;--$_;print $_=" "x$_."*"." "x$_."
";print$_ while(s/ */**/g&&s/* /**/g);'
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
https://pictr.com/images/2022/11/16/Eawj7v.md.png
No for loop, failed. It's just a 1D cellular automata.
"Automata" is a branch of mathematical systems theory, from Wikipedia:
QUOTE Automata play a major role in the theory of computation, compiler construction, artificial intelligence, parsing and formal verification.
Although I do not understand perl script, but I am amazed at how you code them.
Output in my Linux terminal:
Thanks for the inspiring solution.
|
|
|
|
iammyself
|
Nov 16 2022, 06:29 PM
|
Getting Started

|
QUOTE(Fraus @ Nov 16 2022, 02:05 AM) Can I ask in behalf of TS?  Another challenge, similar to original question posed by silverhawk, but this time print a tree (or triangle) with variable size. <a href='https://pictr.com/images/2022/11/16/EadV9c.png' target='_blank'>https://pictr.com/images/2022/11/16/EadV9c.png </a> Of course, you can simply print each line, but what if now you must solve it using for...loop (with variable size input from user). Here's my solution in Go. CODE
package main
import ( "fmt" "strings" )
func main() { fmt.Println("5") printStarTree(5) fmt.Println("10") printStarTree(10) }
func printStarTree(n int) { // Calculate the max number of padded spaces on the left spaceCount := (n - 1)/ 2 var padBuilder strings. Builder for j := 0; j < spaceCount; j++ { padBuilder.WriteRune(' ') } padding := padBuilder.String()
// This string builder is used to accumulate the stars var starBuilder strings. Builder starBuilder.WriteRune('*')
// In each iteration, the padding contracts and the stars expand for i := 1; i <= n; i += 2 { // Print padding + stars fmt.Println(padding[:spaceCount] + starBuilder.String())
// Stars increase at the rate of two per row starBuilder.WriteString("**")
// Reduce padding for the next row spaceCount-- } }
Output This post has been edited by iammyself: Nov 16 2022, 06:31 PM
|
|
|
|
Fraus
|
Nov 16 2022, 08:23 PM
|
New Member
|
QUOTE(iammyself @ Nov 16 2022, 06:29 PM)
Here's my solution in Go.
» Click to show Spoiler - click again to hide... « CODE
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println("5")
printStarTree(5)
fmt.Println("10")
printStarTree(10)
}
func printStarTree(n int) {
// Calculate the max number of padded spaces on the left
spaceCount := (n - 1)/ 2
var padBuilder strings. Builder
for j := 0; j < spaceCount; j++ {
padBuilder.WriteRune(' ')
}
padding := padBuilder.String()
// This string builder is used to accumulate the stars
var starBuilder strings. Builder
starBuilder.WriteRune('*')
// In each iteration, the padding contracts and the stars expand
for i := 1; i <= n; i += 2 {
// Print padding + stars
fmt.Println(padding[:spaceCount] + starBuilder.String())
// Stars increase at the rate of two per row
starBuilder.WriteString("**")
// Reduce padding for the next row
spaceCount--
}
}
Output
https://pictr.com/images/2022/11/16/Eaf8Gr.jpg
Thanks for your unique solution! It is an interesting way to print the tree by counting the stars per row, which is always an odd number.
From my testing, both gives identical result:
CODE
printStarTree(9)
printStarTree(10)
Thumbs up for your solution!
|
|
|
|
angch
|
Nov 16 2022, 09:36 PM
|
|
And here's my solution in Go. https://go.dev/play/p/GiBSWFJpAfgCODE package main
import ( "fmt" "strings" )
func stars(i int) { // minimize the allocs! for buf := []byte(strings.Repeat(" ", i*2+1)); i > 0; i-- { buf[i] = '*' buf[len(buf)-i-1] = '*' fmt.Println(string(buf)) } }
func main() { stars(3) stars(5) stars(10) }
Not quite the same as the Perl version, but the gist is the same: construct the first line of N spaces, one star, and N spaces. And keep repeating printing out each line, adding two stars per iteration, until we can't add any more. The Perl version basically searches and replaces (space, star) with (star, star) and (star, space) with (star, star) every loop until it can't find any (space, star). This post has been edited by angch: Nov 16 2022, 09:43 PM
|
|
|
|
iammyself
|
Nov 16 2022, 10:14 PM
|
Getting Started

|
QUOTE(angch @ Nov 16 2022, 09:36 PM) And here's my solution in Go. https://go.dev/play/p/GiBSWFJpAfgCODE package main
import ( "fmt" "strings" )
func stars(i int) { // minimize the allocs! for buf := []byte(strings.Repeat(" ", i*2+1)); i > 0; i-- { buf[i] = '*' buf[len(buf)-i-1] = '*' fmt.Println(string(buf)) } }
func main() { stars(3) stars(5) stars(10) }
Not quite the same as the Perl version, but the gist is the same: construct the first line of N spaces, one star, and N spaces. And keep repeating printing out each line, adding two stars per iteration, until we can't add any more. The Perl version basically searches and replaces (space, star) with (star, star) and (star, space) with (star, star) every loop until it can't find any (space, star). Oh, I like your approach -- start with spaces and just fill in the stars. Nice work.
|
|
|
|
angch
|
Nov 17 2022, 12:52 PM
|
|
Bonus. Sierpinski's, using same method (Cellular Automata) https://go.dev/play/p/JZ5JVjIfMcDhttps://gist.github.com/angch/831801a7660da...69e8bcd0bb369f6Meh, LYN doesn't like me posting code for some reason. CODE package main
import ( "fmt" "strings" )
func cell(cells []byte, rule int) { t := [8]byte{} for i := 0; i < 8; i++ { switch rule & 1 { case 0: t[i] = ' ' case 1: t[i] = 'X' } rule >>= 1 } for row := 0; row < 100; row++ { fmt.Println(string(cells)) state := 0 if cells[0] == 'X' { state = 1 } for i := 1; i < len(cells); i++ { state = (state << 1) & 7 if cells[i] == 'X' { state = state | 1 } cells[i-1] = t[state] } state = (state << 1) & 7 cells[len(cells)-1] = t[state] } }
func main() { buf := []byte(strings.Repeat(" ", 120))
// single X in the middle buf[len(buf)/2] = 'X' cell(buf, 18) // 18 = 00010010 binary }
This post has been edited by angch: Nov 17 2022, 12:53 PM
|
|
|
|
yunuskk P
|
Nov 17 2022, 03:53 PM
|
New Member
|
CODE #include <stdio.h>
int main(void) { int rows = 0; int i = 0; int j = 0; int Numspace = 0; int Numstar = 0; int Maxstar = 0; printf("Number of Rows : "); scanf("%d", &rows); Maxstar = 2*rows - 1; for(i = 0; i < rows; i++) { Numstar = 2*i + 1; Numspace = (Maxstar - Numstar)/2; if (Numspace > 0) printf("%*c", Numspace, ' '); for(j = 0; j < Numstar; j++) printf("*"); printf("\n");
} return 0; Using c
|
|
|
|
Fraus
|
Nov 17 2022, 05:39 PM
|
New Member
|
QUOTE(angch @ Nov 17 2022, 12:52 PM)
Bonus. Sierpinski's, using same method (Cellular Automata)
Meh, LYN doesn't like me posting code for some reason.
Amazing,  this is beyond my comprehension. I notice you use binary string and bitwise operation.
QUOTE(yunuskk @ Nov 17 2022, 03:53 PM)
Thank you for coming up with a C program, your solution is similar to mine, particularly the part to print stars.
|
|
|
|
iammyself
|
Nov 17 2022, 07:52 PM
|
Getting Started

|
QUOTE(Fraus @ Nov 16 2022, 08:23 PM) Thanks for your unique solution! It is an interesting way to print the tree by counting the stars per row, which is always an odd number. From my testing, both gives identical result: CODE
printStarTree(9)
printStarTree(10)
Thumbs up for your solution!  Oh... I just realized that the input is the number of rows / height of the tree. For some reason I was assuming that the input is the width of the tree. This post has been edited by iammyself: Nov 17 2022, 07:57 PM
|
|
|
|
iammyself
|
Nov 17 2022, 08:22 PM
|
Getting Started

|
QUOTE(iammyself @ Nov 16 2022, 06:29 PM) Here's my solution in Go. Output I couldn't edit my original post. But I've updated my original solution to support height as the input. I really like other forumers solutions and I think they work a little better than my solution, but just for completeness:- CODE
package main
import ( "fmt" "strings" )
func main() { fmt.Println("Width: 5") printStarTreeWidth(5) fmt.Println("Width: 10") printStarTreeWidth(10)
fmt.Println("Height: 3") printStarTreeHeight(3) fmt.Println("Height: 5") printStarTreeHeight(5) }
func printStarTreeHeight(h int) { printStarTreeWidth((2 * h) - 1) }
func printStarTreeWidth(w int) { // Calculate the max number of padded spaces on the left spaceCount := (w - 1)/ 2 padding := strings.Repeat(" ", spaceCount)
// This string builder is used to accumulate the stars var starBuilder strings.Builder starBuilder.WriteRune('*')
// In each iteration, the padding contracts and the stars expand for i := 1; i <= w; i += 2 { // Print padding + stars fmt.Println(padding[:spaceCount] + starBuilder.String())
// Stars increase at the rate of two per row starBuilder.WriteString("**")
// Reduce padding for the next row spaceCount-- } }
This post has been edited by iammyself: Nov 17 2022, 08:23 PM
|
|
|
|
Fraus
|
Nov 17 2022, 08:57 PM
|
New Member
|
QUOTE(iammyself @ Nov 17 2022, 08:22 PM) I couldn't edit my original post. But I've updated my original solution to support height as the input. I really like other forumers solutions and I think they work a little better than my solution, but just for completeness:- » Click to show Spoiler - click again to hide... « CODE
package main
import ( "fmt" "strings" )
func main() { fmt.Println("Width: 5") printStarTreeWidth(5) fmt.Println("Width: 10") printStarTreeWidth(10)
fmt.Println("Height: 3") printStarTreeHeight(3) fmt.Println("Height: 5") printStarTreeHeight(5) }
func printStarTreeHeight(h int) { printStarTreeWidth((2 * h) - 1) }
func printStarTreeWidth(w int) { // Calculate the max number of padded spaces on the left spaceCount := (w - 1)/ 2 padding := strings.Repeat(" ", spaceCount)
// This string builder is used to accumulate the stars var starBuilder strings.Builder starBuilder.WriteRune('*')
// In each iteration, the padding contracts and the stars expand for i := 1; i <= w; i += 2 { // Print padding + stars fmt.Println(padding[:spaceCount] + starBuilder.String())
// Stars increase at the rate of two per row starBuilder.WriteString("**")
// Reduce padding for the next row spaceCount-- } }
That's very clever of you for the quick fix. (My bad for not defining properly the "variable size" in my question, it is not wrong either to do it by width of the tree.)
|
|
|
|