## 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 `for` loop.

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.

Pages: 1 2

### 13 Responses to “Homework”

1. Zack said

Nifty little exercise. The single loop makes it more challenging. Here is my take on it using Julia: https://pastebin.com/mqqywQn6
Stay healthy!

2. Steve said

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"]
```
3. chaw said

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:

``` 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321 1 121 12321 1234321 123454321 12345654321 1234567654321 123456787654321 12345678987654321 ```

4. Daniel said

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:

```1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
```
5. Daniel said

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);
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
12345678987654321
```
6. Jamie Hope said

Another 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);
assert(n >= 1 && n <= 9);

char buf;

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;
}
```
7. gabe said

def updown(n):
return “”.join([f”{n-abs(n-i)}” for i in range(1, 2*n)])

8. Sam Claflin said

Python solution:

Main sort function

def sort_ints(n):
total_nums = n*2 – 1
int_list = []

``````for i in range(1, total_nums + 1):
if i <= n:
int_list.append(i)
else:
int_list.append(int_list[total_nums - i])

return int_list
``````

Print result

print(sort_ints(5))

======================

OUTPUT

[1, 2, 3, 4, 5, 4, 3, 2, 1]

9. Manfred said

VBA Solution:

``````Function UpDownLoop(n As Long) As String
Dim i As Long
UpDownLoop = CStr(n)
For i = 1 To n - 1
UpDownLoop = CStr(n - i) & UpDownLoop & CStr(n - i)
Next i
End Function
``````
10. Karan N said
```#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 temp
```
11. Jan Van lent said

Two 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="")
```
12. Jan Van lent said

@Karan N The hwOne function is neat.

#include<stdio.h>
#include<stdlib.h>
int main()
{
int k;
scanf(“%d”,&k);

``````for(int i=1;i<2*k;i++)
{
if(i<=k)
printf("%d ",i);
else
printf("%d ",i-2*(i%k));
}
return 0;
``````

}