Daily Programming Challenge

Write a program that reverses an integer value using no arrays and strings (or chars)

102214 ==> 412201

Other urls found in this thread:

pastebin.com/raw/wwvdjvEj
pastebin.com/raw/PA9Pbzyc
twitter.com/NSFWRedditVideo

#include
int main()
{
int n, reversedNumber = 0, remainder;

printf("Enter an integer: ");
scanf("%d", &n);

while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}

printf("Reversed Number = %d", reversedNumber);

return 0;
}

nice

Thanks

>mfw I read the code and can't think of original solution
what do?

I think it doesn't work with numbers that end with 0

giving the number as a string or saving the values and iterating through an array with each digit could reverse it with zeroes at the begining

Does not work for numbers ending in zeros. Also does not work for negative numbers.

>negative numbers
Trivial, depending on the original number youd just *= -1

This, 10 won't give 01, although another trivial problem, could hard code a simple fix for numbers ending in 0

Sorry about that, it's roughly 5 years that I don't write a single line of code so yes it might be buggy

maybe printing each digit/remainder as it is computed would solve the problem. instead of creating a new integer with the reversed value.

Lol I just thought of a better solution:

Get the original number, do a bitwise operation logical AND and shift it across, every number you isolate (left to right) keep multiplying by 10

ez katka

You just don't have to.
The %10 is the standard way for this standard pajeet homework.

is that JS code?

n = ~n;

That fizzbuzz isn't even right, it prints fizzbuzz and fizz on a multiple of 15...

?

You really should post problems which allow for a more diverse range of solutions.
Anything that isn't basically the same thing as is just going to be pretty stupid and contrived.

make a thread then

someone write the solution for I cant be fked doing it myself, I wanna see what it looks like

I don't care enough to make one.
I was just making a suggestion for the next time OP posts something.

Im writing a terminal in AS3 :^)

Erlang:
-module(integer).
-export([reverse/1]).

reverse(N) ->
reverse(N, 0).

reverse(0, X) ->
X;
reverse(N, X) ->
reverse(N div 10, X * 10 + N rem 10).

Oh fug forgot to pixelize the path in the title of the window. Whatever.

Nobody cares. It's not like anything can do anything with just your first name.
I've posted screenshots with my name inside my $PS1 dozens of times.

The question states reverse an integer value, not digits, that means there should be no trailing 0

You have to reverse an INTEGER, meaning you call a function that takes an integer, so your computer will treat 00001 as 1 anyway.

this

Yes I just don't want to be identified across threads based on my username and style and terminal. Not like I post that often though.

They got the overthinkers syndrome

noone cared Erik, as long as you're not shitposting constantly

I wanted to make a one-liner. Unfortunately Python isn't very suited to that sort of a thing:

So first I have to define Z combinator.
Z = lambda f: (lambda x: f(lambda *args: x(x)(*args)))(lambda x: f(lambda *args: x(x)(*args)))

Then I can write:
Z(lambda f: lambda a,b: b if not a else x(a//10,b*10+a%10))(int(input()),0)

Yeah, I'm bored. How did you guess?

Here's a challenge: write the shortest possible code to print this piece of text: pastebin.com/raw/wwvdjvEj

...

dd bs=1 count=1943 if=/dev/urandom
It prints the right text, sometimes.

Modifying because for loops look prettier.
uint32_t reverseDigits(uint32_t orig)
{
uint32_t retval;
for(retval = 0; orig != 0; orig /= 10)
retval = (retval * 10) + (orig % 10);

return retval;
}

$ cat challenge.sh
#!/bin/sh
echo "$1" | rev
$ challenge 102214
412201

pastebin.com/raw/PA9Pbzyc

pastebin link because 4chin spam filter doesn't let me post it for some reason

The idea is to come up with your own compression included in the code but ok.

>"Enter an integer: "
>"%d"
>"Reversed Number = %d"
You weren't allowed to use strings motherfucker.

k

#include
#include
#include

void reverse_print(int num)
{
int neg = (num < 0), rev = 0, digits;

num = abs(num);
digits = log10(num) + 1;

for (; num != 0; num /= 10)
rev = rev * 10 + num % 10;

printf(neg ? "%0*d-\n" : "%0*d\n", digits, rev);
}

int main(int argc, char **argv)
{
for (int i = 1; i < argc; i++)
reverse_print(atoi(argv[i]));

return 0;
}

~ $ ./a.out 1234 1230 -234 -230
4321
0321
432-
032-

Gave this one a stab, ran out of time. The basic principle is sound, just have to find the proper fractal.

iwtu=".I just wanna T_ @.Gotta Munderstand."
v=".Z|.Zlet _ down.Zrun aroundAdesert _.ZMcry.Zsay goodbye.ZTa lieAhurt _."
sff=".We've known each other for so long.\
_r heart'sBaching but.\
_Rtoo shy to sL\
Inside we bothKwhat'sBgoing on.\
WeKthe gameAweRgonna plL"
b=".WeRno strangers to love._Kthe rulesAso do I.a full commitment's whatXthinking of"+\
"._ wouldn't get this from any other guy"""+iwtu+v+sff+"""and if _ ask me @
Don't Tme _Rtoo blind to see."""+v+v+"."+sff+iwtu+v*3

rwo="B been -Lay it.-Mmake _ -R're -K know -A and -Ttell ->#).ZQ, ZQ.(|-

echo file_get_contents("pastebin.com/raw/wwvdjvEj");

op that was really boring, do something cool with bitwise math next time

int intrev(int num)
{
int rev = 0;
while (num)
{
rev += num % 10;
rev *= 10;
num /= 10;
}
return rev / 10;
}

this is more computationally expensive than simply using an array of chars.