redtails 2 months ago [hidden] python#%% rows = 5 middle rows // 2 star_counter = 1 for i in range(0,rows): #Decrease amount of stars, increase spaces if i >= middle: print(' ' * (i - middle) + '*' * star_counter) star_counter -= 2 #Increase amount of stars, decrease spaces else: print(''* (middle - i) + '*'* star_counter) star_counter += 2 JS (n) => Array(n).fill().map((_, i) => '*'.repeat(i < n/2 ? i * 2 + 1 : (n - i) * 2 - 1).padStart(i < n/2 ? (n/2) - i + i * 2 + 1 : n + (n/2) - i, ' ')).join('\n') 17 Reply Copy ID Copy Link
Marto Sepa 2 months ago [hidden] ANSI C#include <stdio.h> #include <stdlib.h> /* TODO: handle even values */ #define ROWS 5 #define MID (ROWS / 2) int main(void) { int row; for (row = 0; row < ROWS; row += 1) { int col; int star = 2 * (MID - abs(MID - row)) + 1; for (col = 0; col < abs(MID - row); col += 1) { fputc(' ', stdout); } for (col = 0; col < star; col += 1) { fputc('*', stdout); } fputc('\n', stdout); } return EXIT_SUCCESS; } 12 Reply Copy ID Copy Link
Fhtagn 2 months ago [hidden] Zig (v0.11.0)// print_the_stars.zig const std = @import("std"); test "Print the stars" { std.debug.print("\n", .{}); inline for (.{ comptime generateStars(5), comptime generateStars(6), comptime generateStars(7), comptime generateStars(30), }) |stars| { printStars(stars); } } fn generateStars(comptime stars: usize) []const []const u8 { comptime { const is_even_number = stars % 2 == 0; const actual_size = if (is_even_number) stars - 1 else stars; var arrays: [actual_size][]const u8 = undefined; const middle: isize = stars / 2; const string_format = std.fmt.comptimePrint("{{s: ^{d}}}", .{stars}); var i: usize = if (is_even_number) 1 else 0; for (&arrays, i..) |*array, j| { const number_of_stars = 2 * (middle - std.math.absCast(middle - j)) + if (is_even_number) 0 else 1; array.* = std.fmt.comptimePrint(string_format, .{"*" ** number_of_stars}); } return &arrays; } } fn printStars(stars: anytype) void { std.debug.print("Printing {d} stars:\n", .{stars[0].len}); for (stars) |star| { std.debug.print("{s}\n", .{star}); } std.debug.print("\n", .{}); }Output$ zig version 0.11.0 $ zig test print_the_stars.zig Test [1/1] test.Print the stars... Printing 5 stars: * *** ***** *** * Printing 6 stars: ** **** ****** **** ** Printing 7 stars: * *** ***** ******* ***** *** * Printing 30 stars: ** **** ****** ******** ********** ************ ************** **************** ****************** ******************** ********************** ************************ ************************** **************************** ****************************** **************************** ************************** ************************ ********************** ******************** ****************** **************** ************** ************ ********** ******** ****** **** ** All 1 tests passed. 6 Reply Copy ID Copy Link
Allexedge 2 months ago [hidden] Sinclair BASIC (man, haven't touched it for 30 years or so)10 LET r=5 20 LET m=INT (0.5+r/2) 30 DIM e$(r-m) 40 DIM s$(r) 50 FOR i=1 TO r: LET s$(i)="*": NEXT i 60 FOR i=1 TO r 70 LET d=ABS (m-i) 80 PRINT e$(1 TO d)+s$(1 TO r-2*d) 90 NEXT i 9 Reply Copy ID Copy Link