Convert List Of Numbers To One Integer
Solution 1:
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.
Solution 2:
With a loose interpretation of "builtins":
defrem_odd(integer):
returnint(''.join(x for x instr(integer) ifnot x in'13579'))
Applying a stricter definition of builtins:
def rem_odd(integer):
r = 0
mult = 1whileinteger > 0:
ifinteger % 2 == 0:
r = r + mult * (integer % 10)
integer = integer// 10
mult *= 10return r
~
Solution 3:
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))
returnNULL;
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];
}
returnPy_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, Extensionrem_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 262014, 12:01:28)
[GCC 4.8.220140206 (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
Solution 4:
If you can't use built in functions like int() or join(), you can do it like this:
def rem_odd(integer):
x = []
whileinteger > 0:
ifinteger % 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
Solution 5:
Integers can be split up into digits raised to a power of 10.
For instance:
- 24 = 2 * 10 + 4
- 256 = 2 * 100 + 5 * 10 + 6
You can utilize the position in your list to determine your current power of 10:
defgetInt(lst):
res = 0pow = len(lst) - 1for el in lst:
res += el * (10 ** pow)
pow -= 1return 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.
Post a Comment for "Convert List Of Numbers To One Integer"