Write a program that reverses an integer value using no arrays and strings (or chars)
102214 ==> 412201
>>59095198#include <stdio.h>
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;
}
>>59095225
nice
>>59095236
Thanks
>>59095225
>mfw I read the code and can't think of original solution
what do?
>>59095225
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
>>59095225
Does not work for numbers ending in zeros. Also does not work for negative numbers.
>>59095292
>negative numbers
Trivial, depending on the original number youd just *= -1
>>59095267
This, 10 won't give 01, although another trivial problem, could hard code a simple fix for numbers ending in 0
>>59095267
>>59095292
>>59095313
Sorry about that, it's roughly 5 years that I don't write a single line of code so yes it might be buggy
>>59095313
>>59095355
maybe printing each digit/remainder as it is computed would solve the problem. instead of creating a new integer with the reversed value.
>>59095355
>>59095372
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
>>59095263
You just don't have to.
The %10 is the standard way for this standard pajeet homework.
>>59095418
is that JS code?
>>59095403n = ~n;
>>59095418
That fizzbuzz isn't even right, it prints fizzbuzz and fizz on a multiple of 15...
>>59095465
?
>>59095198
You really should post problems which allow for a more diverse range of solutions.
Anything that isn't basically the same thing as >>59095225 is just going to be pretty stupid and contrived.
>>59095568
make a thread then
>>59095568
someone write the solution for >>59095403
I cant be fked doing it myself, I wanna see what it looks like
>>59095581
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 :^)
>>59095198
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).
>>59095832
Oh fug forgot to pixelize the path in the title of the window. Whatever.
>>59095842
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.
>>59095292
The question states reverse an integer value, not digits, that means there should be no trailing 0
>>59095267
>>59095292
>>59095313
>>59095355
>>59095372
You have to reverse an INTEGER, meaning you call a function that takes an integer, so your computer will treat 00001 as 1 anyway.
>>59095897
this
>>59095876
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.
>>59095939
They got the overthinkers syndrome
>>59095955
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: http://pastebin.com/raw/wwvdjvEj
>>59096168
<?=file_get_contents("http://pastebin.com/raw/wwvdjvEj"))?>
>>59096168dd bs=1 count=1943 if=/dev/urandom
It prints the right text, sometimes.
>>59096486
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;
}
>>59095198$ cat challenge.sh
#!/bin/sh
echo "$1" | rev
$ challenge 102214
412201
>>59096168
http://pastebin.com/raw/PA9Pbzyc
pastebin link because 4chin spam filter doesn't let me post it for some reason
>>59096609
The idea is to come up with your own compression included in the code but ok.
>>59095225
>"Enter an integer: "
>"%d"
>"Reversed Number = %d"
You weren't allowed to use strings motherfucker.
>>59095267
>>59095313
k#include <stdio.h>
#include <stdlib.h>
#include <math.h>
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-
>>59096168
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.(|-<#, |)-|Q _ up-@howXfeeling-#.(Ooh-Qgive-Znever gonna -X I'm -_you-.\n"
for i in rwo.split("-"): b=b.replace(i[0], i[1:])
print(string.capwords(b, "\n"))
>>59096168echo file_get_contents("http://pastebin.com/raw/wwvdjvEj");
>>59095198
op that was really boring, do something cool with bitwise math next timeint intrev(int num)
{
int rev = 0;
while (num)
{
rev += num % 10;
rev *= 10;
num /= 10;
}
return rev / 10;
}
>>59101441
>>59095225
>>59095198
this is more computationally expensive than simply using an array of chars.