python-3.x 如何检查给定的数字或列表是频闪数字?

6ioyuze2  于 2023-10-21  发布在  Python
关注(0)|答案(3)|浏览(136)

一个频闪数是一个数字,当我们旋转180度时,它应该和原来的数字一样。

a= [6,9]
b=a

for n, i in enumerate(a):
     if i == 0:
         a[n] = 0
     elif i == 1:
        a[n] = 1
     elif i == 8:
        a[n] = 8
     elif i == 6:
        a[n] = 9
     elif i == 9:
        a[n] = 6

a.reverse()
if (a == b ):
    print("True")
else:
    print("False")

我希望69的输出为True,35的输出为False。

2uluyalo

2uluyalo1#

请参阅geeksforgeeks的解释

# Pyhton program to print all 
# Strobogrammatic number of length n 

# strobogrammatic function 
def strobogrammatic_num(n): 

    result = numdef(n, n) 
    return result 

# definition function 
def numdef(n, length): 

    if n == 0: return [""] 
    if n == 1: return ["1", "0", "8"] 

    middles = numdef(n - 2, length) 
    result = [] 

    for middle in middles: 
        if n != length:          
            result.append("0" + middle + "0") 

        result.append("8" + middle + "8") 
        result.append("1" + middle + "1") 
        result.append("9" + middle + "6") 
        result.append("6" + middle + "9") 
    return result 

# Driver Code 
if __name__ == '__main__': 

    # Print all Strobogrammatic 
    # number for n = 3 
    print(strobogrammatic_num(3))
bvhaajcl

bvhaajcl2#

你可以在java中找到这个。

import java.util.*;
public class Solution {
 public static void main(String[] args) {
  String n = "9006";
  System.out.println("Is " + n + " is Strobogrammatic? " + is_Strobogrammatic(n));
 }

 public static boolean is_Strobogrammatic(String n) {
  if (n == null || n.length() == 0) {
   return true;
  }
  HashMap < Character, Character > map = new HashMap < Character, Character > ();
  map.put('0', '0');
  map.put('1', '1');
  map.put('8', '8');
  map.put('6', '9');
  map.put('9', '6');
  int left = 0;
  int right = n.length() - 1;
  while (left <= right) {
   if (!map.containsKey(n.charAt(right)) || n.charAt(left) != map.get(n.charAt(right))) {
    return false;
   }
   left++;
   right--;
  }
  return true;
 }
}
kwvwclae

kwvwclae3#

下面是JavaScript版本

function StrobogrammaticNum(num) {
  
  // create a lookup for identifying counter numbers 
  const lookup = [
    { key: 0, value: 0},
    { key: 1, value: 1},
    { key: 8, value: 8},
    { key: 6, value: 9},
    { key: 9, value: 6}
  ]
  
  // create an array of numbers
  const arrNum = String(num).split('').map(x => Number(x))

  // set default result to true
  let isStrobogrammaticNum = true;
  
  // iterate through the array of each sides and check if the other side has matching value to a key - keep in mind that for odd number of element, the division by 2 results in a non integer which will let the loop run an extra time 
  for(let i=0; i < arrNum.length/2; i++ ) {

    // check if number qualifies for Strobogram
    const findMatchingNum = lookup.find(x=> x.key === arrNum[i]);
    
    // if the number is not Strobogram or if the counter part is not the lookup value, set the result variable to false
    if(!findMatchingNum || (findMatchingNum && arrNum[arrNum.length -(i+1)] !== findMatchingNum.value)) {
      console.log(arrNum[arrNum.length -1])
      isStrobogrammaticNum = false 
    }
  } 

  return isStrobogrammaticNum;
}

console.log(StrobogrammaticNum(18069081)) // returns true
console.log(StrobogrammaticNum(1806081)) // returns false

相关问题