如何在不使用Python

时间:2017-09-25 13:08:22

标签: python string integer int

我需要它做家庭作业,我们有一位新老师说我们应该谷歌但我没有找到有用的答案。

我尝试转换例如:a =“546”到a = 546,没有任何库函数

提前感谢您的帮助!

7 个答案:

答案 0 :(得分:4)

"最纯粹的"我能想到:

>>> a = "546"
>>> result = 0
>>> for digit in a:
        result *= 10
        for d in '0123456789':
            result += digit > d

>>> result
546

如果允许,请使用@ Ajax1234的字典构思:

>>> a = "546"
>>> value = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}
>>> result = 0
>>> for digit in a:
        result = 10 * result + value[digit]

>>> result
546

答案 1 :(得分:2)

您可以保留一个存储数字键的字符串和整数值的字典,然后迭代字符串。迭代字符串时,您可以使用enumerate来跟踪索引,然后将10增加到该幂减1,然后乘以字典中的相应键:

a = "546"
length = 0
for i in a:
   length += 1
d = {'1': 1, '0': 0, '3': 3, '2': 2, '5': 5, '4': 4, '7': 7, '6': 6, '9': 9, '8': 8}
count = 0
counter = 0
for i in a:
   count += (10**(length-counter-1)*d[i])
   counter += 1
print(count)

输出:

546

答案 2 :(得分:1)

诀窍是546 = 500 + 40 + 65*10^2 + 4*10^1 + 6*10^0

注意指数只是索引(反向)。使用它,您可以将此方法概括为函数:

def strToInt(number):
    total = 0                             # this is where we accumulate the result
    pwr = len(number) - 1                 # start the exponent off as 2
    for digit in number:                  # digit is the str "5", "4", and "6"
        digitVal = ord(digit) - ord('0')  # using the ascii table, digitVal is the int value of 5,4, and 6.
        total += digitVal * (10 ** pwr)   # add 500, then 40, then 6
        pwr -= 1                          # make sure to drop the exponent down by one each time
    return total

你可以像这样使用它:

>>> strToInt("546")
546

答案 3 :(得分:1)

def stringToInt(s):
    result = 0
    value = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
    for digit in s:
        result = 10 * result + value[digit]

    return result

答案 4 :(得分:0)

您可以循环使用字符串并使用ord对每个字符执行操作。

示例:

a="546"
num=0
for i in a:
     num = num * 10 + ord(i) - ord('0')

答案 5 :(得分:0)

<script language="JavaScript">
 function toggle(source) {
  checkboxes = document.getElementsByName('foo');
  for(var i=0, n=checkboxes.length;i<n;i++) {
    checkboxes[i].checked = source.checked;
  }
}
</script>

<input type="checkbox" onClick="toggle(this)" /> Toggle All<br/>

<input type="checkbox" name="foo" value="bar1"> Monday<br/>
<input type="checkbox" name="foo" value="bar2"> Tuesday<br/>
<input type="checkbox" name="foo" value="bar3"> Everyday<br/>
<input type="checkbox" name="foo" value="bar4"> Days that don't end in 'Y'<br/>

答案 6 :(得分:0)

def int(a):
ty = a.__class__.__name__
out = 0
di = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
      '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
if ty not in ("str", "int", "float", "bytes"):
    raise TypeError("unsupported format")
if a.__class__ == float:
    return a.__floor__()
elif a.__class__ == int:
    return a
else:
    ind = 0
    for val in a[::-1]:
        if val not in di:
            raise ValueError("invalid input")
        out += di[val]*(10**ind)
        ind += 1
        #print(out, di[val])
    return out
print(int("55"))
55