Homework
March 20, 2020
Today’s exercise is somebody’s homework:
Write a program that displays the digits from 1 to n then back down to 1; for instance, if n = 5, the program should display 123454321. You are permitted to use only a single
forloop.
The questioner did not specify what should happen when n reaches 10, so we will specify 0 < n < 10.
Your task is to write the requested program; if you like, think of other ways to write that program. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Nifty little exercise. The single loop makes it more challenging. Here is my take on it using Julia: https://pastebin.com/mqqywQn6
Stay healthy!
Klong version
!5 [0 1 2 3 4] 1+!5 [1 2 3 4 5] $1+!5 ["1" "2" "3" "4" "5"] ,/$1+!5 "12345" s::,/$1+!5 "12345" (-1)_s::,/$1+!5 "1234" |(-1)_s::,/$1+!5 "4321" s,|(-1)_s::,/$1+!5 "123454321" {[s];s,|(-1)_s::,/$1+!x}'1+!9 ["1" "121" "12321" "1234321" "123454321" "12345654321" "1234567654321" "123456787654321" "12345678987654321"]Here are three simple versions in R7RS Scheme.
Pedantic note: I realize that the solution by @programmingpraxis does
not claim conformance to any specific Scheme standard. However, if
portability or conformance to standard Scheme is desired then I
believe that the integers cannot be handed as-is (e.g., without
conversion to strings) to the display procedure. My reading of R7RS
gives implementations a free hand when displaying objects that are not
strings or chars. In my very quick testing, there is at least one
major implementation that will output a space after ‘display’-ing each
integer.
(import (scheme base) (scheme write)) (define (display-digit-stream n) ;; (assert (< 0 n 10)) (do ((i 1 (+ i 1))) ((>= i (* 2 n))) (display (number->string (if (<= i n) i (- (* 2 n) i))))) (newline)) (define (demo proc) (for-each proc '(1 2 3 4 5 6 7 8 9))) (demo display-digit-stream) (define (display-digit-stream/1 n) ;; (assert (< 0 n 10)) (do ((i 1 (+ i step)) (step (if (= 1 n) -1 1) (if (= i (- n 1)) -1 step))) ((< i 1)) (display (number->string i))) (newline)) (demo display-digit-stream/1) (define (display-digit-stream/2 n) ;; (assert (< 0 n 10)) (let loop ((i 1) (step (if (= n 1) -1 1))) (when (positive? i) (display (number->string i)) (loop (+ i step) (if (= i (- n 1)) -1 step)))) (newline)) (demo display-digit-stream/2)Output:
Here are a few solutions in Python.
from itertools import chain def homework1(n): return '123456789'[:n] + '876543210'[-n:-1] def homework2(n): return ''.join(str(x if x <= n else n - x % n) for x in range(1, 2 * n)) def homework3(n): return ''.join(str(x) for x in chain(range(1, n + 1), range(n - 1, 0, -1))) for n in range(1, 10): assert homework1(n) == homework2(n) == homework3(n) print(homework1(n))Output:
Here’s a solution in C.
#include <assert.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { assert(argc == 2); int n = atoi(argv[1]); assert(n >= 1 && n <= 9); for (int i = 1; i < 2 * n; ++i) { printf("%d", i <= n ? i : n - i % n); } printf("\n"); return EXIT_SUCCESS; }Example:
$ for x in {1..9}; do ./a.out $x; done 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321Another solution in C, with n loop iterations instead of 2n:
#include <assert.h> #include <stdio.h> #include <stdlib.h> int main(int argc, const char* argv[]) { /* Borrowed from Daniel's */ assert(argc == 2); int n = atoi(argv[1]); assert(n >= 1 && n <= 9); char buf[18]; for (int i = 1; i <= n; ++i) { buf[i-1] = buf[n*2-i-1] = '0'+i; } printf("%.*s\n", n*2-1, buf); return 0; }def updown(n):
return “”.join([f”{n-abs(n-i)}” for i in range(1, 2*n)])
Python solution:
Main sort function
def sort_ints(n):
total_nums = n*2 – 1
int_list = []
Print result
print(sort_ints(5))
======================
OUTPUT
[1, 2, 3, 4, 5, 4, 3, 2, 1]
VBA Solution:
#No Loops taking, single multiplication calculation def hwOne(n): return int("1" * n)**2 #Single loop def hwTwo(n): temp = [] for i in range(1,2*n): if(i<n+1): temp.append(i) else: temp.append(2*n-i) return tempTwo Python solutions that use abs or min instead of using an if statement directly.
def f(n): for i in range(1, 2*n): print(n - abs(n - i), end="") def g(n): for i in range(1, 2*n): print(min(i, 2*n - i), end="")@Karan N The hwOne function is neat.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int k;
scanf(“%d”,&k);
}