Homework
March 20, 2020
I suspect the instructor was thinking of something like this, with one loop:
(define (f1 n)
(do ((i 1 (+ i 1))) ((= i (* 2 n)))
(display (if (< n i) (- (* 2 n) i) i)))
(newline))
The student probably had this in mind, with two loops:
(define (f2 n) (do ((i 1 (+ i 1))) ((= n i)) (display i)) (do ((i n (- i 1))) ((= 0 i)) (display i)) (newline))
Of course, you could use recursion with zero loops:
(define (f3 n)
(define (f n s)
(if (= n 1) (begin (display s) (newline))
(f (- n 1) (string-append (number->string (- n 1)) s (number->string (- n 1))))))
(f n (number->string n)))
Since the limit on n is so small, here is a cheeky solution that has no loops and does no computation:
(define (f4 n)
(display (case n
((1) "1")
((2) "121")
((3) "12321")
((4) "1234321")
((5) "123454321")
((6) "12345654321")
((7) "1234567654321")
((8) "123456787654321")
((9) "12345678987654321")))
(newline))
You can run the program at https://ideone.com/aeCJjV.
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);
}