找回密码
 立即注册
查看: 630|回复: 0

checkio之旅

[复制链接]

18

主题

0

回帖

58

积分

版主

积分
58
发表于 2023-5-4 18:41:54 | 显示全部楼层 |阅读模式
在之前学习中,深刻认识到只靠书是不行的,所以便在checkio这个网站敲代码。
这段时间完成的只是最基本的Home里的任务,后续也会慢慢去解锁其他的关卡。
1.House Password
条件:Stephan and Sophia forget about security and use simple passwords for everything. Help Nikola develop a password security check module. The password will be considered strong enough if its length is greater than or equal to 10 symbols, it has at least one digit, as well as containing one uppercase letter and one lowercase letter in it. The password contains only ASCII latin letters or digits.
  1. def checkio(data):
  2.     test = []
  3.     if len(data)>=10:
  4.         for i in data:
  5.             if i.islower():
  6.                 test.append(0)
  7.             elif i.isupper():
  8.                 test.append(1)
  9.             elif i.isdigit():
  10.                 test.append(2)
  11.         if len(set(test)) ==3:
  12.             return True
  13.     else:
  14.         return False
复制代码
思路:判断data中每一个元素的类型,小写在test中添加0;大写添加1;数字添加2。
2.The Most Wanted Letter
条件:You are given a text, which contains different english letters and punctuation symbols. You should find the most frequent letter in the text. The letter returned must be in lower case.
While checking for the most wanted letter, casing does not matter, so for the purpose of your search, "A" == "a". Make sure you do not count punctuation symbols, digits and whitespaces, only letters.
If you have two or more letters with the same frequency, then return the letter which comes first in the latin alphabet. For example -- "one" contains "o", "n", "e" only once for each, thus we choose "e".
  1. def checkio(text):
  2.     text = text.lower()
  3.     a = {}
  4.     b = []
  5.     for i in text:
  6.         if 97<=ord(i)<=122:
  7.             a[i] = text.count(i)
  8.     value = 0
  9.     key = 0
  10.     for k,v in a.items():
  11.         if v>value:
  12.             value = v
  13.             key = k
  14.         elif v==value:
  15.             if ord(key) > ord(k):
  16.                 key = k
  17.         else:
  18.             pass
  19.     return key
复制代码
思路:现将所有字母变成小写,之后将字母和其呈现次数放入字典,接着进行斗劲
3.Non-unique Elements
条件:你将得到一个含有整数(X)的非空列表。在这个任务里,你应该返回在此列表中的非独一元素的列表。要做到这一点,你需要删除所有独特的元素(这是包含在一个给定的列表只有一次的元素)。解决这个任务时,不能改变列表的挨次。例如:[1,2,3,1,3] 1和3长短独一元素,成果将是 [1, 3, 1, 3]。
  1. def checkio(data):
  2.     D = []
  3.     for i in data:
  4.         if data.count(i)>1:
  5.             D.append(i)
  6.     return D
复制代码
思路:按照呈现次数判断
4.Monkey Typing
条件:Let&#39;s suppose our monkeys are typing, typing and typing, and have produced a wide variety of short text segments. Let&#39;s try to check them for sensible word inclusions.
You are given some text potentially including sensible words. You should count how many words are included in the given text. A word should be whole and may be a part of other word. Text letter case does not matter. Words are given in lowercase and don&#39;t repeat. If a word appears several times in the text, it should be counted only once.
For example, text - "How aresjfhdskfhskd you?", words - ("how", "are", "you", "hello"). The result will be 3.
  1. def count_words(text, words):
  2.     t = text.lower()
  3.     str = []
  4.     for i in words:
  5.         if i in t:
  6.             str.append(i)
  7.     return len(str)
复制代码
思路:直接对比
5.Xs and Os Referee
条件:Tic-Tac-Toe, sometimes also known as Xs and Os, is a game for two players (X and O) who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal rows (NW-SE and NE-SW) wins the game.
But we will not be playing this game. You will be the referee for this games results. You are given a result of a game and you must determine if the game ends in a win or a draw as well as who will be the winner. Make sure to return "X" if the X-player wins and "O" if the O-player wins. If the game is a draw, return "D".
  1. def checkio(shuru):
  2.     row = []
  3.     for i in range(3):
  4.         if &#39;XXX&#39; in shuru[i]:
  5.             return &#39;X&#39;
  6.         elif &#39;OOO&#39; in shuru[i]:
  7.             return &#39;O&#39;
  8.         else:
  9.             pass
  10.         row.append(list(shuru[i]))
  11.     ver = list(zip(row[0],row[1],row[2]))
  12.     for i in range(3):
  13.         if len(set(ver[i]))==1:
  14.             if &#39;X&#39; in set(ver[i]):
  15.                 return &#39;X&#39;
  16.             elif &#39;O&#39; in set(ver[i]):
  17.                 return &#39;O&#39;
  18.         else:
  19.             pass
  20.     if shuru[0][0]==shuru[1][1]==shuru[2][2]==&#39;X&#39;:
  21.         return &#39;X&#39;
  22.     elif shuru[0][0]==shuru[1][1]==shuru[2][2]==&#39;O&#39;:
  23.         return &#39;O&#39;
  24.     elif shuru[0][2]==shuru[1][1]==shuru[2][0]==&#39;X&#39;:
  25.         return &#39;X&#39;
  26.     elif shuru[0][2]==shuru[1][1]==shuru[2][0]==&#39;O&#39;:
  27.         return &#39;O&#39;
  28.     else:
  29.         return &#39;D&#39;
复制代码
思路:分三种情况:横着的、竖着的、斜着的
6.Pawn Brotherhood
条件:Chess is a two-player strategy game played on a checkered game board laid out in eight rows (called ranks and denoted with numbers 1 to 8) and eight columns (called files and denoted with letters a to h) of squares. Each square of the chessboard is identified by a unique coordinate pair — a letter and a number (ex, "a1", "h8", "d6"). For this mission we only need to concern ourselves with pawns. A pawn may capture an opponent&#39;s piece on a square diagonally in front of it on an adjacent file, by moving to that square. For white pawns the front squares are squares with greater row than their.
A pawn is generally a weak unit, but we have 8 of them which we can use to build a pawn defense wall. With this strategy, one pawn defends the others. A pawn is safe if another pawn can capture a unit on that square. We have several white pawns on the chess board and only white pawns. You should design your code to find how many pawns are safe.
  1. def safe_pawns(pawns):
  2.     coordinate = []
  3.     for i in pawns:
  4.         row = ord(i[0])
  5.         ver = int(i[1])
  6.         coordinate.append([row,ver])
  7.     count = 0
  8.     for i in coordinate:
  9.         if [i[0]-1,i[1]-1] in coordinate:
  10.             count +=1
  11.         elif [i[0]+1,i[1]-1] in coordinate:
  12.             count +=1
  13.     return count
复制代码
思路:将棋子换成坐标,用坐标进行判断
7.Long Repeat
条件:This mission is the first one of the series. Here you should find the length of the longest substring that consists of the same letter. For example, line "aaabbcaaaa" contains four substrings with the same letters "aaa", "bb","c" and "aaaa". The last substring is the longest one which makes it an answer.
  1. def long_repeat(line):
  2.     count = 0
  3.     a = {}
  4.     if len(line)!=0:
  5.         for i in range(len(line)-1):
  6.             if line[i] == line[i+1]:
  7.                 count+=1
  8.                 a[line[i]] = count+1
  9.             else:
  10.                 count = 0
  11.         b=list(a.items())
  12.         if len(b)>1:
  13.             t= max(a,key = a.get)
  14.             return a[t]
  15.         elif len(b)==1:
  16.             return b[0][1]
  17.         else:
  18.             return 1
  19.     else:
  20.         return 0
复制代码
思路:按照其子字符串进行判断
8.All the Same
条件:In this mission you should check if all elements in the given list are equal.
  1. def all_the_same(list):
  2.     a = set(list)
  3.     if len(a)==1 or len(a)==0:
  4.         return True
  5.     else:
  6.         return False
复制代码
上面的任务我基本上是靠简单的for循环和if语句完成的,python强大的内置函数和相关库都没用,checkio里大神的答案基本上一两句就完成了任务(而且checkio里不懂的,发到论坛里,很快就有人回答了),所以,日后的学习任重而道远啊!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|T9AI - 深度人工智能平台 ( 沪ICP备2023010006号 )

GMT+8, 2024-12-4 16:28 , Processed in 0.061401 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表