I want to produce the following string of numbers:
1,2,3 ... n ... 3,2,1
Is there an elegant and simple way to do it?
I want to produce the following string of numbers:
1,2,3 ... n ... 3,2,1
Is there an elegant and simple way to do it?
Depend entirely on what you want to do with it. Typing 1,2,3, ... , n, ... 3, 2, 1 is a pretty good way of just "producing" it.
n = 100
OK. 1, 2, 3, ... 100, 3, 2, 1
There.
Again, it depends on what you want to do with it.
nice, you forgot 96*2 numbers though
what??? like coding? what are you trying to say to me OhPee?!?!?!????
they are assumed with the "..."
X =1;
For(;X=1;X--)
For int("%d,",X);
Something like this should do it
You're welcome for doing your homework.
n-|n-i| for the i-th number
>needing two cycles
that's not elegant and simple
OP never said it was a programming assignment. He just said he wanted to "produce" those numbers.
So OP, you could carve them into a piece of wood, write them down, count them out loud, take n steps and then n steps backwards, etc.
Now this is elegant and simple, thanks
Top kek
He's right tho
If your starting i is a negative number
hiding a check in a conditional is just deceiving yourself that you've achieved simplicity
I mean hiding a check in an absolute value
No? Let's say n is 10, and my starting i is -12.
10 - |(10 - (-12))| = 10 - (22) = -12
Let's say i is -2 then:
10 - |(10 - (-2))| = 10 - 12 = -2
It literally just produces i. That's the joke.
Maybe I misunderstood what you meant though.
Your solution is correct, I just means it's not simpler than actually writing an IF statement
Getting the absolute value is as simple as adding a 0 in the righ-most position of a string of bytes
There's no if involved
Do you even machine code?
i is just an index so it can't be negative. But yeah you're right, I assumed it always started at 1.
n-|n-i|+j-1
where j is the starting number.
in your case it would be -12
10-|10-i|-13 for the i-th number
so the first number in the sequence would be
10-|10-1|-13=-12
...
Ah, right
thank god for haskell
[code]
make n = map show $ [1..n] ++ [n-1,n-2..1]
[/code]
not elegant or simple
print str(range(n)+reversed(range(n-1)))
>not simple
. brainlet
>probably uses a hundred of unnecessary clock cycles
>simple
the only other one here is more complicated and the result is longer
That's because your programming language is garbage
Try it in assembly
>1,2,3 ... n ... 3,2,1
Oh, that's just [math]\{(A_0, A_1, A_2, ... , A_n) \, | \, A_0 = 1, \, A_1 = 2, \, A_2 = 3, \, A_{n-2} = 3, \, A_{n-1} = 2, \, A_n = 1\}.[/math]
pls do this in assembly
[code]
rpn = head . foldl f [] . words
where f (x:y:stk) "-" = y - x : stk
f (x:y:stk) "+" = y + x : stk
f (x:y:stk) "*" = y * x : stk
f (x:y:stk) "/" = y / x : stk
f (x:stk) "log" = log x : stk
f stk n = read n : stk
[/code]
>[code]
not an argument
>wanting to "produce" a mathematical abstraction that cannot exist in the corporeal realm
lol
k = 1;
b = 0;
while(k>0)
{
print "k ";
b = b+1;
k = k + (-1)^(b/100);
}
[1:n,n-1:1] or is it [1:n;n-1:1] in MATLAB
#include "stdafx.h"
#include
using namespace std;
int main()
{
cout > number;
cout
How'd I do senpai?
int[] counter = new int[2*num - 1];
for(int i = 1; i
ls = []
t = []
ls.extend(range(0, 100))
t.extend(range(0, 99))
t.reverse()
ls.extend(t)
print (ls)
[code]
int n;
printf("Type n: ");
scanf("%d", &n);
int a = 0;
int add = 1;
do{
a += add;
if(a != 0){
printf("%d", a);
}
if(a == n){
add = -1;
}
} while(a > 0);
[/code]
I tried. Haven't used C in a while.
Python and two loops:
[code]
n = input("Type n: ")
for a in range(n):
print a + 1
for a in range(n-1):
print n-a
[/code]
Ah fuck, no code tags on Veeky Forums? Lame.
Take your pedophile cartoons back to and your programming homework back to .
Fucking degenerate.