本文整理汇总了Python中string.ascii_uppercase.index函数的典型用法代码示例。如果您正苦于以下问题:Python index函数的具体用法?Python index怎么用?Python index使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了index函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: kill_ship
def kill_ship(self, ship):
ship = ship.upper()
if self.field[((ascii_uppercase.index(ship[0]) * self.size) + int(ship[1]) - 1)] == 0:
self.field[((ascii_uppercase.index(ship[0]) * self.size) + int(ship[1]) - 1)] = '_'
return True
else:
return False
开发者ID:alex7088,项目名称:tceh-python,代码行数:7,代码来源:models.py
示例2: caeser
def caeser(sourcetext, offset=None, reverse=False):
# caeser(string) -> string
# offset - integer value for cipher offset
# reverse - bool to reverse the offset (useful for decryption)
# Apply an offset to ascii characters.
# Trivial case, return sourcetext unchanged and avoid
# translating character by character.
if offset == None:
return sourcetext
# reverse flag undoes the cipher offset. This is the same
# as making the offset negative. Conditional merely changes
# the sign of offset. The same effect can be achieved by
# manually adjusting the sign of offset in the caller.
if reverse:
offset = -offset
# build enciphered string character by character
# For each character, if it is an ascii letter apply
# the offset and append the new character to the cipher.
# Otherwise, simply append nonletters to the cipher.
cipher = []
for char in sourcetext:
if char in ascii_letters:
if char in ascii_lowercase:
i = ascii_lowercase.index(char) + offset
i = modulate_index(ascii_lowercase, i)
cipher.append(ascii_lowercase[i])
else:
i = ascii_uppercase.index(char) + offset
i = modulate_index(ascii_uppercase, i)
cipher.append(ascii_uppercase[i])
else: cipher.append(char)
ciphertext = ''.join(cipher)
return ciphertext
开发者ID:justinoneil,项目名称:foobar,代码行数:35,代码来源:caeser.py
示例3: columnIndex
def columnIndex(column):
try:
column = int(column) - 1
except:
column = ascii_uppercase.index(column)
return column
开发者ID:AustinW,项目名称:cpsc-481-assignment1,代码行数:7,代码来源:Square.py
示例4: moving_shift
def moving_shift(string, shift):
words = string
lens = len(words)
m, n = divmod(lens, 5)
if n > 0:
m +=1
res = ['', '', '', '' ,'']
chars = -1
for idx, char in enumerate(words):
chars += 1
change=None
if char in ascii_lowercase + ascii_uppercase:
if char in ascii_lowercase:
pos = ascii_lowercase.index(char)
np = (pos + shift + chars) % 26
change = ascii_lowercase[np]
elif char in ascii_uppercase:
pos = ascii_uppercase.index(char)
np = (pos + shift + chars) % 26
change = ascii_uppercase[np]
else:
change = char
res[chars//m] += change
return res
开发者ID:NaomiRison,项目名称:Leetcode,代码行数:25,代码来源:First-variation-on-caesar-cipher.py
示例5: get_name_value
def get_name_value(name):
"""
get value of a name
"""
value = sum([ascii_uppercase.index(letter) + 1
for letter in name])
return value
开发者ID:lycheng,项目名称:project_euler,代码行数:7,代码来源:p21_p30.py
示例6: make_diamond
def make_diamond(letter):
lines = []
length = ascii_uppercase.index(letter) + 1
for l in range(length):
line = [' '] * length
line[l] = ascii_uppercase[l]
lines.append(''.join(list(reversed(line)) + line[1:]))
return '\n'.join(lines[:-1] + list(reversed(lines))) + '\n'
开发者ID:ThomasZumsteg,项目名称:exercism-python,代码行数:8,代码来源:diamond.py
示例7: alpha_to_int
def alpha_to_int(alpha):
try:
return ascii_uppercase.index(alpha.upper())+1
except ValueError:
if alpha == ' ':
return 0
else:
raise WrongCharError
开发者ID:darvin,项目名称:brainspell,代码行数:8,代码来源:numerology.py
示例8: value
def value( name ):
"""
>>> from euler22 import value
>>> value("COLIN")
53
"""
chars= [ ascii_uppercase.index(c)+1 for c in name ]
return sum( chars )
开发者ID:slott56,项目名称:my-euler,代码行数:8,代码来源:euler22.py
示例9: ltr2nbr
def ltr2nbr( s ):
"""
>>> from euler42 import ltr2nbr
>>> ltr2nbr("SKY")
[19, 11, 25]
>>> sum(ltr2nbr("SKY"))
55
"""
return [ 1+ascii_uppercase.index(c) for c in s ]
开发者ID:slott56,项目名称:my-euler,代码行数:9,代码来源:euler42.py
示例10: rotate
def rotate(message, key):
coded_message = ""
for char in message:
if char in alpha_lower:
char = alpha_lower[(alpha_lower.index(char) + key) % ALPHA_LEN]
elif char in alpha_upper:
char = alpha_upper[(alpha_upper.index(char) + key) % ALPHA_LEN]
coded_message += char
return coded_message
开发者ID:fortrieb,项目名称:python,代码行数:9,代码来源:example.py
示例11: caeser_cipher
def caeser_cipher(text, key):
encrypted = ''
for i in text:
if i in l:
encrypted += l[(l.index(i) + key) % 26]
elif i in u:
encrypted += u[(u.index(i) + key) % 26]
else:
encrypted += i
return encrypted
开发者ID:sr1k4n7h,项目名称:HackerRank,代码行数:10,代码来源:caesar_cipher.py
示例12: lex
def lex(word):
score,i = [],0
while i <= 15:
if i < len(word):
value = abc.index(word[i])
value = str(value) if value > 9 else "0"+str(value)
score.append(value)
else:
score.append("00")
i = i + 1
return int(reduce(lambda a,b: a+b,score))
开发者ID:nicksebasco,项目名称:Project-Euler,代码行数:11,代码来源:p22.py
示例13: encryptor
def encryptor(key, message):
key %= 26
result = []
for a in message:
if a.islower():
result.append(low[(low.index(a) + key) % 26])
elif a.isupper():
result.append(up[(up.index(a) + key) % 26])
else:
result.append(a)
return ''.join(result)
开发者ID:the-zebulan,项目名称:CodeWars,代码行数:11,代码来源:dbftbs_djqifs.py
示例14: checkio
def checkio(number):
to_int = lambda x: int(x) if x.isdigit() else alpha.index(x) + 10
digits = list(map(to_int, number))
k = max(digits) + 1
for k in itertools.count(k):
if k > MAX_K:
return 0
else:
decimal = sum((k ** i) * x for i, x in enumerate(reversed(digits)))
if not decimal % (k - 1):
return k
开发者ID:aminami1127,项目名称:checkio,代码行数:11,代码来源:radix.py
示例15: caesar
def caesar(s, shift):
result = []
for a in s:
if a.isalpha():
if a.islower():
result.append(low[(low.index(a) + shift) % 26])
else:
result.append(up[(up.index(a) + shift) % 26])
else:
result.append(a)
return ''.join(result)
开发者ID:the-zebulan,项目名称:CodeWars,代码行数:11,代码来源:caesar_cipher.py
示例16: _alpha_value
def _alpha_value(chars):
"""Calculate the alphabetical value (as described by `Problem 22`_).
.. _Problem 22: http://projecteuler.net/index.php?section=problems&id=22
>>> _alpha_value('COLIN')
53
>>> _alpha_value('RUTH')
67
"""
return sum((1 + ascii_uppercase.index(char) for char in chars))
开发者ID:gthank,项目名称:project-euler-workspace,代码行数:11,代码来源:problem22.py
示例17: play_pass
def play_pass(string, n):
result = []
for i, a in enumerate(string):
if a.isdigit():
char = str(9 - int(a))
elif a.isalpha():
char = up[(up.index(a) + n) % 26]
char = char if i % 2 == 0 else char.lower()
else:
char = a
result.append(char)
return ''.join(reversed(result))
开发者ID:the-zebulan,项目名称:CodeWars,代码行数:12,代码来源:playing_with_passphrases.py
示例18: rotate
def rotate(text, degrees):
# Building this dictionary is almost certainly a preoptimization given the
# lengths of the test inputs.
rotate_dict = {}
for l in ascii_lowercase:
rotated_index = (ascii_lowercase.index(l) + degrees) % NUM_LETTERS
rotate_dict[l] = ascii_lowercase[rotated_index]
for l in ascii_uppercase:
rotated_index = (ascii_uppercase.index(l) + degrees) % NUM_LETTERS
rotate_dict[l] = ascii_uppercase[rotated_index]
return sub(r'(.)', lambda l: rotate_dict.get(l.group(1), l.group(1)), text)
开发者ID:ebiven,项目名称:exercism,代码行数:12,代码来源:rotational_cipher.py
示例19: triangle_words
def triangle_words(file, c=0):
triangles = {x*(x+1)/2 for x in xrange(1000)}
value = {c: ascii_uppercase.index(c)+1 for c in ascii_uppercase}
with open(file, 'r') as f:
words = list(f.read().replace('"', '').split(','))
for word in words:
s = 0
for char in word:
s += value[char]
if s in triangles:
c += 1
return c
开发者ID:Zane-,项目名称:project_euler,代码行数:12,代码来源:042.py
示例20: getPixelWidth
def getPixelWidth(self, sentence, uppercase, lowercase):
l = 0
letters = 0
for i in sentence:
if i in U:
l += uppercase[U.index(i)]
elif i in L:
l += lowercase[L.index(i)]
else:
l += 3
letters += 1
l += letters-1
print letters, len(sentence)
return l
开发者ID:phrayezzen,项目名称:TopCoder,代码行数:14,代码来源:265.py
注:本文中的string.ascii_uppercase.index函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论