命理学..如何缩短代码..?

时间:2017-06-12 14:28:02

标签: python

我已经编写了这段代码来查找名称的数字值。有什么方法可以缩短代码,或者在另一个内部嵌套一个循环?

alphabets = {"A":1,"I":1,"J":1, "Q":1,"Y":1,"B":2,"K":2,"R":3,"C":3,"G":3,"L":3,"S":3,"D":4,"M":4,"T":4,"H":5,"E":5,"N":5,"X":5,"U":6,"V":6,"W":6,"O":7, "Z":7,"P":8,"F":8}

word =input("your numerology score is :") #since i am using python 3 to code this

def digit_sum(n):
    #prepare a list of numbers in n convert to string and reconvert
    numbers=[]
    for digit in str(n):
        numbers.append(int(digit))
    # add up the total of numbers    
    total=0
    for number in numbers:
        total += number
    return total  

def numerology(word):
        total = 0
        for letter in word.upper():
            total += alphabets[letter]
            total = digit_sum(total)

        return total

print (numerology(word))

要了解数字学价值的含义,请参阅https://en.wikipedia.org/wiki/Numerology#Latin_alphabet_systems

5 个答案:

答案 0 :(得分:2)

alphabets = {"A":1,"I":1,"J":1, "Q":1,"Y":1,"B":2,"K":2,"R":3,"C":3,"G":3,"L":3,"S":3,"D":4,"M":4,"T":4,"H":5,"E":5,"N":5,"X":5,"U":6,"V":6,"W":6,"O":7, "Z":7,"P":8,"F":8}

name = "this is a sample name"

digits = str(sum([alphabets[l] for l in name.upper() if l in alphabets.keys()]))

numerological_value = int(digits) % 9

if numerological_value == 0:
    numerological_value = 9

print(numerological_value)

答案 1 :(得分:2)

理解允许您创建各种数据类型。 在这种情况下,您想要构建生成器。 这是因为您不需要在将列表减少到单个数字之前构建每个数字。

sum中包含一个可以让您显着缩短digit_sum。哪个可以成为:

def digit_sum(n):
    return sum(int(digit) for digit in str(n))

如果将加法和赋值结合起来,也可以将数字命理改为更短。

def numerology(word):
    total = 0
    for letter in word.upper():
        total = digit_sum(total + alphabets[letter])
    return total

如果需要,可以使用functools.reduce使此跨度总共为六行。

def numerology(word):
    return functools.reduce(
        lambda a, b: sum(int(digit) for digit in str(a + b)),
        (alphabets[letter] for letter in word.upper()),
        0
    )

答案 2 :(得分:1)

笑声的单行。

alphabets = {"A":1,"I":1,"J":1, "Q":1,"Y":1,"B":2,"K":2,"R":3,"C":3,"G":3,"L":3,"S":3,"D":4,"M":4,"T":4,"H":5,"E":5,"N":5,"X":5,"U":6,"V":6,"W":6,"O":7, "Z":7,"P":8,"F":8}
print("Your numerology score is "+ str((int(sum([alphabets[l] for l in input("Type your name:").upper() if l in alphabets.keys()])) % 9) or 9))

答案 3 :(得分:1)

这一个!!!

int main()
{
    int k,l;
    system("cls");
    printf("\n\n \t\t ** WELCOME TO C NOTEPAD ** \n\n START FROM HERE  -  :) \n\n    ");
    while(1)
{
    k=getch();
    if(k==0xE0)  // TO check if we enter arrow keys
    {
        l=getch();
        switch(l)
        {
            case 75 : printf("\b"); break;  // LEFT ARROW
            //case 77 : printf("\r"); break;  //RIGHT ARROW

            default:  continue;
        }
    }
    else
    {
         switch(k)
        {
            case 13 : printf("\n"); break;  // New line after pushing ENTER key
            case 8 : printf("\b"); break;   // BACK SPACE
            case 9 : printf("\t"); break;   // TAB
            case 27: system("cls");         // Clear screen after pushing ESCAPE key

                    printf("\n\n * PRESS ANY KEY TO START AGAIN \t *PRESS 'ESC' TO EXIT");
                            if(getch()==27)
                                exit(0);
                            else
                                main();

            case 127 :  printf(" "); break; // DELETE 

            default:   printf("%c",k);
        }
    }
}
return 0;

gematria

答案 4 :(得分:0)

我确定您可以更好地链接化简器功能,但是...

const charMap = {A:1, J:1, S:1, B:2, K:2, T:2, C:3, L:3, U:3, D:4, 
  M:4, V:4, E:5, N:5, W:5, F:6, O:6, X:6, G:7, P:7, Y:7, H:8, 
  Q:8, Z:8, I:9, R:9};

let name = prompt ("Type your name in CAPS"); //for example: TOM

let wordScore =  Array.from(name).reduce((nameScore, element) => {
    let curValue = charMap[element]
     return (nameScore + curValue)
  },0)

let finalScore = Array.from(String(wordScore), Number).reduce((score, element) => {

  return score > 10 ? score + element : score
})


alert(finalScore)