Spike8086 2021-04-15 16:08 采纳率: 0%
浏览 2163

Typeerror:argument of type 'int' is not iterable

import random,time,math,gc
def get_level_entropy(level):
  MAX_ENTROPY=256
  difficulty_entropy=MAX_ENTROPY**(-level)
  return difficulty_entropy
def block_check_repeat(map,value,i_pos,j_pos):
  assert map!=None
  start_hand_pos_j=0 if j_pos==0 else(round((j_pos+1)/3)-1)*3
  start_hand_pos_i=0 if i_pos==0 else(round((i_pos+1)/3)-1)*3
  #print(f"i:{i_pos} j:{j_pos}")
  #print(f"i_bolck:{start_hand_pos_i} j_block:{start_hand_pos_j}")

  for step_i in range(0,3):
    for step_j in range(0,3):
      if start_hand_pos_j+step_j!=j_pos and start_hand_pos_i+step_i!=i_pos:
        if map[start_hand_pos_j+step_j][start_hand_pos_i+step_i]==value:
          return False
  return True
def cross_line_check_repeat(map,value,i_pos,j_pos):
  for step_i in range(0,9):
    for step_j in range(0,9):
      if (map[j_pos][step_i]==value or map[step_j][i_pos]==value) and (step_j!=j_pos and step_i!=i_pos):
        return False
  return True
def block_revise_error(map,value,i_pos,j_pos):
  sudo_map=map
  assert map!=None
  start_hand_pos_j=0 if j_pos==0 else(round((j_pos+1)/3)-1)*3
  start_hand_pos_i=0 if i_pos==0 else(round((i_pos+1)/3)-1)*3
  #print(f"i:{i_pos} j:{j_pos}")
  #print(f"i_bolck:{start_hand_pos_i} j_block:{start_hand_pos_j}")
  for step_i in range(0,9):
    for step_j in range(0,9):
      if start_hand_pos_j+step_j!=j_pos and start_hand_pos_i+step_i!=i_pos:
        if map[start_hand_pos_j+step_j][start_hand_pos_i+step_i]==value:
          sudo_map[start_hand_pos_j+step_j][start_hand_pos_i+step_i]=0
  return sudo_map
def get_value_index(map,value):
  assert map!=None
  j=0
  j_list,i_list=[],[]
  for list in map:
    #print(list)
    if value in list:
      #print(f"valye{value}")
      i_list.append(list.index(value))
      j_list.append(j)
    j+=1

  return [j_list,i_list]
def loop_protector(value,up_limit,down_limit=0):
    return (value>up_limit) or (value<down_limit)
def sudo_num_counter(map):
  count=0
  for list in map:
    for element in list:
      if not(element==0):
        count+=1
  return count
def information_entropy(map):
  sum_entropy=0
  for i in range(0,9):
    for j in range(0,9):
      num_check_list=[]
      for inter in range(1,10):
          if map[j][i]==0 and block_check_repeat(map,inter,i,j) and cross_line_check_repeat(map,inter,i,j):
            num_check_list.append(inter)
      for k in range(1,10):
           #print(num_check_list)
           #print(sum_entropy)
           single_possibility=1 if len(num_check_list)==0 else float(1/len(num_check_list))
           sum_entropy-=single_possibility*math.log(single_possibility,2)
  return sum_entropy
def current_timer():
  return time.time()
def show_sudo_map(sudo_map):
  for list in sudo_map:
    print(list)
def block_revise_total(map):
  sudo_map=map
  for value in range(1,10):
    k_counter=len(get_value_index(map,value)[0])
    #print(f"k_counter:{k_counter}")
    try:
      for k_num in range(k_counter):
        j_pos=get_value_index(map,value)[0][k_num]
        i_pos=get_value_index(map,value)[1][k_num]
        #print(f"value:{value} pos:{(j_pos,i_pos)}")
        assert map!=None
        start_hand_pos_j=0 if j_pos==0 else(round((j_pos+1)/3)-1)*3
        start_hand_pos_i=0 if i_pos==0 else(round((i_pos+1)/3)-1)*3
        for step_i in range(0,3):
          for step_j in range(0,3):
            if start_hand_pos_j+step_j!=j_pos and start_hand_pos_i+step_i!=i_pos:
              if map[start_hand_pos_j+step_j][start_hand_pos_i+step_i]==value:
                sudo_map[start_hand_pos_j+step_j][start_hand_pos_i+step_i]=0
    except IndexError:
      return sudo_map
  return sudo_map
def generate_sudo(difficult_level):
  sudo_map=[[0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0],
            [0,0,0,0,0,0,0,0,0]]
  write_counter,main_counter,stride=0,0,0
  map_list=[]
  while write_counter<81:
    for value in range(1,10):
      i_single=random.randint(0,8)
      j_single=random.randint(0,8)
      if sudo_map[j_single][i_single]==0:
        if cross_line_check_repeat(sudo_map,value,i_single,j_single)and block_check_repeat(sudo_map,value,i_single,j_single):
            sudo_map[j_single][i_single]=value
            map_list.append(sudo_map)
            stride+=random.randint(0,1)
            write_counter+=1
        else:
          #print(f'index:{len(map_list)-stride}')
          if len(map_list)-stride-1>0:
            sudo_map=map[len(map_list)-stride] if stride==0 else map_list[len(map_list)-stride-1]
            map=map_list[0:len(map_list)-stride]
            block_revise_total(sudo_map)
            write_counter-=1
    if loop_protector(main_counter,1000):
      if 0 in sudo_map:
        block_revise_total(sudo_map)
      else:
       break
    #print(f"main_counter:{main_counter}")
    #show_sudo_map(sudo_map)
    #print('----------------------------------')
    main_counter+=1
  return sudo_map
def solve_sudo_normal(map):
  sudo_map=map
  write_counter,main_counter,stride=0,0,0
  map_list=[]
  while not(0 in sudo_map):
    for value in range(1,10):
      i_single=random.randint(0,8)
      j_single=random.randint(0,8)
      if sudo_map[j_single][i_single]==0:
        if cross_line_check_repeat(sudo_map,value,i_single,j_single)and block_check_repeat(sudo_map,value,i_single,j_single):
              sudo_map[j_single][i_single]=value
              map_list.append(sudo_map)
              write_counter+=1
        else:
            #print(f'index:{len(map_list)-stride}')
            if len(map_list)-stride-1>0:
              sudo_map=map[len(map_list)-stride] if stride==0 else map_list[len(map_list)-stride-1]
              map=map_list[0:len(map_list)-stride]
              block_revise_total(sudo_map)
              stride+=random.randint(0,1)
              write_counter-=1
    if loop_protector(main_counter,1000):
      if 0 in sudo_map:
       block_revise_total(sudo_map)
      else:
       break
    #print(f"main_counter:{main_counter}")
    main_counter+=1
  return sudo_map


out_map=generate_sudo(100)
previous_time=current_timer()
print(f"num_count:{sudo_num_counter(out_map)}")
print(f"entropy:{information_entropy(out_map)}")
show_sudo_map(out_map)
print('-------------------------------------')
map_solved=solve_sudo_normal(out_map)
print(f"num_count:{sudo_num_counter(map_solved)}")
print(f"entropy:{information_entropy(map_solved)}")
show_sudo_map(map_solved)
current_time=current_timer()
time_gap=current_time-previous_time
print(f'using time{time_gap}s')
gc.collect()

只要告诉我怎么解决这单一error就行,我觉得可能是引用库版本的问题,但是以前没有这样的错误。如果有任何发现还请告诉我,万分感谢(不要在意代码风格,没有什么注释)

  • 写回答

3条回答 默认 最新

  • 关注

    iterator遍历的参数是整形的,没办法进行遍历,再检查一下代码。

    评论

报告相同问题?

悬赏问题

  • ¥15 做数电题要具体的步骤
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真
  • ¥15 关于#c语言#的问题,请各位专家解答!
  • ¥15 这个如何解决详细步骤