Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 Frontend Developers, what's happening?

views
     
angch
post Sep 24 2021, 02:10 PM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
WHHHHHEEE! It's code golfing time! Sorry, offtopic.

CODE

~/build/golf$ cat golf.pl
@_=(1..<>);while(@_){print join(" ",@_),"\n";pop@_}
~/build/golf$ perl golf.pl
5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1


52 bytes!

CODE
$_=<>;print join" ",(1..$_--),"\n"while$_;


43 bytes!

This post has been edited by angch: Sep 24 2021, 03:25 PM
angch
post Mar 17 2022, 11:54 AM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
It's a very roundabout way to ask what is
CODE
.join
, which I used in my Perl solution.

CODE
a = [1, 2, 3]
console.log(a.join(", "));


It's Javascript, so there's tricks you can do with
CODE
JSON.stringify
as well. (Ok, maybe not for this question)

This post has been edited by angch: Mar 17 2022, 12:07 PM
angch
post Nov 16 2022, 11:54 AM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
CODE
% echo 10 | perl -e'$_=<>;--$_;print $_=" "x$_."*"." "x$_."\n";print$_ while(s/ \*/\*\*/g&&s/\* /\*\*/g);'
        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************
*******************

user posted image

No for loop, failed. It's just a 1D cellular automata.

This post has been edited by angch: Nov 16 2022, 12:01 PM
angch
post Nov 16 2022, 09:36 PM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
And here's my solution in Go. wink.gif
https://go.dev/play/p/GiBSWFJpAfg

CODE
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
angch
post Nov 17 2022, 12:52 PM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
Bonus. Sierpinski's, using same method (Cellular Automata)

user posted image

https://go.dev/play/p/JZ5JVjIfMcD

https://gist.github.com/angch/831801a7660da...69e8bcd0bb369f6

Meh, 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
angch
post Nov 20 2022, 08:03 PM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
QUOTE(FlierMate @ Nov 20 2022, 02:39 PM)
If any of you want another challenge, can try print the diamond programmatically.

user posted image
*
"Challenge", tongue.gif C'mon, make it a real challenge.
angch
post Nov 21 2022, 04:22 PM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
user posted image

Alright, Mr Assembly, take this!

150 bytes. DOS 16bit .EXE

It grabs the number of diamonds from the command line.

user posted image

https://gist.github.com/angch/d82694d44c7af...787d944034ff719
angch
post Nov 21 2022, 05:48 PM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
QUOTE(FlierMate @ Nov 21 2022, 04:44 PM)
Very nice! I am aware that you know low-level stuff, but I didn't know you can code in Assembly (i8086)! Fantastic!

I didn't look up interrupt list, how did you get command line argument in DOS? Through "di" register? I know in Windows (through API) and Linux (through stack), but don't know about DOS.

150 bytes is not the smallest,  biggrin.gif   Because it wrap it with MZ header.
I think you already knew, you can produce .COM in FASM by changing the first line to:
CODE
org 100h    


Now it is 118 bytes! Yeah.

I notice the largest size is 12 (turn to 25 rows), because you understand that the DOS terminal window is only 25 rows. It shows you pay attention to detail.

Again, nice one! (Mine is in C#, I am reluctant to do any algorithmic coding using Assembly)
*
I wasn't used to FASM. Was a TASM/MASM dude. Had to relearn a lot of things. Plenty of things I can do to optimize it further, but it was just a quick hack.

There was a few magic memory locations you'd need to know for asm in DOS: CS:80h is the len of the args, CS:81h onwards is the command line argument. 255 (Edit: 20h, not 100h) max chars, of course. 0B800h is the text mode buffer, etc.

This post has been edited by angch: Nov 21 2022, 07:59 PM
angch
post Feb 11 2023, 10:29 AM

On my way
****
Junior Member
635 posts

Joined: Jul 2006
QUOTE(15cm @ Feb 11 2023, 01:13 AM)
woi woi woi its meant to be an insider secret!
*
Not a fan. Too much noise, too little code. I need more Noctua.

 

Change to:
| Lo-Fi Version
0.0235sec    0.24    6 queries    GZIP Disabled
Time is now: 29th March 2024 - 08:39 AM