Reputation: 21
Im trying to take in an integer, remove all the odd digits within the integer, and then return this new integer. NO BUILT-INS. the way i did it so far made it into a list, but i need it to output an integer.
def rem_odd(integer):
x = []
while integer > 0:
if integer % 2 == 0:
x = [(integer % 10)] + x
integer = integer / 10
else:
integer = integer / 10
return x
Upvotes: 0
Views: 802
Reputation: 18457
Since none of the other answers here have managed to do this in compliance with "NO BUILT-INS" in the strictest sense, I submit the following extension to Python for OP's use case:
/* rem_odd.c */
#include <Python.h>
#include <math.h>
static PyObject *
rem_odd(PyObject *self, PyObject *args)
{
int digit, ix, n, num, result;
int digits[20]; /* handles up to 2^64 (20 digits) */
if (!PyArg_ParseTuple(args, "i", &num))
return NULL;
n = num;
ix = 0;
while (n > 0) {
digit = n % 10;
n /= 10;
if (digit % 2 == 0)
digits[ix++] = digit;
}
result = 0;
while (ix-- >= 0) {
result += (int)(pow(10., ix)) * digits[ix];
}
return Py_BuildValue("i", result);
}
static PyMethodDef RemOddMethods[] = {
{"rem_odd", rem_odd, METH_VARARGS,
"Remove odd digits from an integer and return another integer."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initrem_odd(void)
{
(void) Py_InitModule("rem_odd", RemOddMethods);
}
Then using distutils
:
from distutils.core import setup, Extension
rem_odd_module = Extension('rem_odd',
sources = ['rem_odd.c'])
setup(name='rem_odd', version='1.0',
description='Remove odd digits from integers -- NO BUILTINS',
ext_modules = [rem_odd_module])
Example usage (after running setup.py build
and setup.py install
):
Python 2.7.6 (default, Feb 26 2014, 12:01:28)
[GCC 4.8.2 20140206 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from rem_odd import rem_odd
>>> rem_odd(203)
20
>>> rem_odd(9684684)
684684
>>> rem_odd(123456789)
2468
Upvotes: 1
Reputation: 78
Integers can be split up into digits raised to a power of 10.
For instance:
You can utilize the position in your list to determine your current power of 10:
def getInt(lst):
res = 0
pow = len(lst) - 1
for el in lst:
res += el * (10 ** pow)
pow -= 1
return res
Calling >>> getInt([2,4,8])
now returns: 248
Getting the length of your list without the built-in len would be fairly trivial, as you can just do it recursively.
Upvotes: 0
Reputation: 12092
Let's say you invoke your function with 203
as argument:
x = rem_odd(203)
you get x
= [2, 0]
This is what you do then:
>>> x = [2, 0]
>>> x = [str(a) for a in x]
>>> x
['2', '0']
>>> int(''.join(x))
20
>>>
The above is a string manipulation solution. Arithmetically (and not using built-ins), the solution would be:
>>> x = rem_odd(203)
>>> p = y = 0
>>> for i in x[::-1]:
... y += i * (10 ** p)
... p += 1
...
>>> y
20
You traverse the list from right to left, i.e., from units place to the highest power of ten and then multiply each digit with 10 to the power of index.
Upvotes: 5
Reputation: 113824
With a loose interpretation of "builtins":
def rem_odd(integer):
return int(''.join(x for x in str(integer) if not x in '13579'))
Applying a stricter definition of builtins:
def rem_odd(integer):
r = 0
mult = 1
while integer > 0:
if integer % 2 == 0:
r = r + mult * (integer % 10)
integer = integer // 10
mult *= 10
return r
~
Upvotes: 1
Reputation: 8614
If you can't use built in functions like int() or join(), you can do it like this:
def rem_odd(integer):
x = []
while integer > 0:
if integer % 2 == 0:
x = [(integer % 10)] + x
integer = integer / 10
else:
integer = integer / 10
res = 0
digits = len(x)
for i in range(digits):
res += x[i] * (10**(digits-i-1))
return res
Upvotes: 0