Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 Frontend Developers, what's happening?

views
     
iammyself
post Nov 17 2022, 09:40 PM

Getting Started
**
Junior Member
223 posts

Joined: May 2011
QUOTE(Fraus @ Nov 17 2022, 08:57 PM)
That's very clever of you for the quick fix.  thumbup.gif

(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.)
*
Nah, I should have clarified. Anyway, thanks for posting the question.
FlierMate
post Nov 20 2022, 02:39 PM

Casual
***
Junior Member
495 posts

Joined: Nov 2020
If any of you want another challenge, can try print the diamond programmatically.

user posted image
FlierMate
post Nov 20 2022, 02:41 PM

Casual
***
Junior Member
495 posts

Joined: Nov 2020
My solution for the star tree, in C#:

CODE
using System;
                                       
public class Program
{
       public static void Main()
       {
               int size=6;
               for (int a=0; a<=size; a++)
               {
                       for (int b=size; b>a; b--)
                               Console.Write(" ");
                       
                       for (int c=1; c<a*2; c++)
                               Console.Write("*");
                       
                       for (int b=size; b>a; b--)
                               Console.Write(" ");
                       
                       Console.WriteLine();
               }
       }
}

angch
post Nov 20 2022, 08:03 PM

On my way
****
Junior Member
538 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.
iammyself
post Nov 21 2022, 01:16 PM

Getting Started
**
Junior Member
223 posts

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

user posted image
*
I'll play! But before we begin... what's the input parameter? icon_idea.gif
FlierMate
post Nov 21 2022, 02:10 PM

Casual
***
Junior Member
495 posts

Joined: Nov 2020
QUOTE(iammyself @ Nov 21 2022, 01:16 PM)
I'll play! But before we begin... what's the input parameter?  icon_idea.gif
*
Honestly I don't know the standard input parameter. Personally I use the upper half as the size (from middle to top), which is easier when coding.

Thank you for willing to play. smile.gif

This post has been edited by FlierMate: Nov 21 2022, 02:10 PM
angch
post Nov 21 2022, 04:22 PM

On my way
****
Junior Member
538 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
FlierMate
post Nov 21 2022, 04:44 PM

Casual
***
Junior Member
495 posts

Joined: Nov 2020
QUOTE(angch @ Nov 21 2022, 04:22 PM)
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
*
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)
angch
post Nov 21 2022, 05:48 PM

On my way
****
Junior Member
538 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
iammyself
post Nov 21 2022, 09:24 PM

Getting Started
**
Junior Member
223 posts

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

user posted image
*
Here's my submission in Go! The code supports both height and width as the input.

I can't seem to get the formatting to work below. But you can see the code in action here: https://go.dev/play/p/tuerhNZt2wr

CODE

// ASCII codes for asterisk and space
const (
star  = 42
space = 32
)

func printDiamondH(h int) {
if h%2 == 0 {
 printDiamondW(h + 1)
} else {
 printDiamondW(h)
}
}
func printDiamondW(w int) {
row := make([]byte, w)
for i, _ := range row {
 row[i] = space
}

// Starting positions for the star(s)
l, r := w/2, w/2
if w%2 == 0 {
 l = r - 1
} // When the width is even, the first and last row will have 2 stars instead of 1

// Grow downward until the middle row
for l >= 0 && r < len(row) {
 row[l], row[r] = star, star

 fmt.Println(string(row))

 row[l], row[r] = space, space // reset the array

 l--
 r++
}

// Manually move the pointers so the middle row isn't printed twice
l = 1
r = len(row) - 2

// Shrink it back
for l <= r {
 row[l], row[r] = star, star

 fmt.Println(string(row))

 row[l], row[r] = space, space // reset the array

 l++
 r--
}
}


Output:

user posted image

This post has been edited by iammyself: Nov 21 2022, 09:28 PM
FlierMate
post Nov 21 2022, 10:11 PM

Casual
***
Junior Member
495 posts

Joined: Nov 2020
QUOTE(iammyself @ Nov 21 2022, 09:24 PM)
Here's my submission in Go! The code supports both height and width as the input.

I can't seem to get the formatting to work below. But you can see the code in action here: https://go.dev/play/p/tuerhNZt2wr

» Click to show Spoiler - click again to hide... «

Output:

user posted image
*
Thank you for the code, thumbsup.gif looks simpler but working.

Now I think I slowly understand Go:
CODE
for l >= 0 && r < len(row) { ......

for l <= r {.....

....is actually a WHILE...LOOP, not FOR...NEXT.

I like the way you reposition the star at both end with "l--" & "r++" or "r--" and "l++".

Unlike my solution, you print one line at a time, which is neater.


FlierMate
post Nov 21 2022, 10:15 PM

Casual
***
Junior Member
495 posts

Joined: Nov 2020
I hope @silverhawk wouldn't mind for further discussion about my coding challenge.

I will start new topic for coding challenge (if any) in the future.
Sorry for hijacking your thread.
iammyself
post Nov 22 2022, 03:59 AM

Getting Started
**
Junior Member
223 posts

Joined: May 2011
QUOTE(FlierMate @ Nov 21 2022, 10:11 PM)
Thank you for the code,  thumbsup.gif  looks simpler but working.

Now I think I slowly understand Go:
CODE
for l >= 0 && r < len(row) { ......

for l <= r {.....

....is actually a WHILE...LOOP, not FOR...NEXT.

I like the way you reposition the star at both end with "l--" & "r++" or "r--" and "l++".

Unlike my solution, you print one line at a time, which is neater.
*
Yup! In Go, while loops are implemented using the for statement.

9 Pages « < 7 8 9Top
 

Change to:
| Lo-Fi Version
0.0128sec    0.21    5 queries    GZIP Disabled
Time is now: 27th November 2022 - 06:43 AM