python - Python bit.ly 链接词表生成器
问题描述
我一直在开发一个 Python 工具来生成 bit.ly 单词表。以下是 bit.ly 链接的特殊性:
- 包含 7 个实体
- 以数字开头(通常为 3 或 2)
- 以一封信结尾
- 不能并排拥有相同的实体
我已经做了前三个条件,但我找不到最后一个条件。
from itertools import product
def firstN(chars, length):
for firstNumber in product(chars, repeat=length):
yield ''.join(firstNumber)
def combiwords(chars, length):
for letters in product(chars, repeat=length):
yield ''.join(letters)
def lastL(chars, length):
for lastLetter in product(chars, repeat=length):
yield ''.join(lastLetter)
def main():
firstNumber = "32"
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
lastLetter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
for wordlen1 in range(1, 2):
for first in firstN(firstNumber, wordlen1):
for wordlen2 in range(6, 7):
for combo in combiwords(letters, wordlen2):
for wordlen3 in range(1, 2):
for word in lastL(lastLetter, wordlen3):
print('https://bit.ly/' + first + combo + word)
if __name__=="__main__":
main()
解决方案
这可能是您正在搜索的内容:
https://www.geeksforgeeks.org/recursively-remove-adjacent-duplicates-given-string/
def removeUtil(string, last_removed):
# If length of string is 1 or 0
if len(string) == 0 or len(string) == 1:
return string
# Remove leftmost same characters
# and recur for remaining
# string
if string[0] == string[1]:
last_removed = ord(string[0])
while len(string) > 1 and string[0] ==
string[1]:
string = string[1:]
string = string[1:]
return removeUtil(string, last_removed)
# At this point, the first
# character is definiotely different
# from its adjacent. Ignore first
# character and recursively
# remove characters from remaining string
rem_str = removeUtil(string[1:], last_removed)
# Check if the first character
# of the rem_string matches
# with the first character of
# the original string
if len(rem_str) != 0 and rem_str[0] ==
string[0]:
last_removed = ord(string[0])
return (rem_str[1:])
# If remaining string becomes
# empty and last removed character
# is same as first character of
# original string. This is needed
# for a string like "acbbcddc"
if len(rem_str) == 0 and last_removed ==
ord(string[0]):
return rem_str
# If the two first characters of
# str and rem_str don't match,
# append first character of str
# before the first character of
# rem_str.
return ([string[0]] + rem_str)
def remove(string):
last_removed = 0
return toString(removeUtil(toList(string),
last_removed))
# Utility functions
def toList(string):
x = []
for i in string:
x.append(i)
return x
def toString(x):
return ''.join(x)
将当前输出存储到列表中(例如wl
)
wl.append('https://bit.ly/' + first + combo + word)
并在您的循环remove
遍历每个条目之后。
推荐阅读
- r - 将“Ymw”转换为日期 - 一周的第一个星期一
- ssl - 我正在尝试将 ESP8266 与 HTTPS python 服务器连接,但 ESP 未与服务器连接并给出服务器名称错误的 Bearssl 错误代码
- c# - ASP.NET Core appsettings.json reloadOnChange 不适用于符号链接文件?
- python - 如何让我的机器人发送图像以及如何创建关闭它的命令?
- laravel - 为什么我得到一个整数而不是数组?
- cmake - CMake MacOS 通用二进制构建:链接到某个库仅适用于 x86_64,不适用于 ARM
- angular - 如何在 Angular/Jasmine 中测试 Filereader.onload 回调函数?
- java - 为什么jmeter不能解析哈希字符?
- database - 使用不同的用户和密码创建多个 PostgreSQL 数据库
- r - 将数字的命名向量乘以R中的单个数字