给定一个形成单词句子的字符数组,给出一个有效的算法来反转其中单词(而不是字符)的顺序.
示例输入和输出:
>>> reverse_words("this is a string")
'string a is this'
Run Code Online (Sandbox Code Playgroud)
它应该是O(N)时间和O(1)空间(split()并且不允许推入/弹出堆栈).
这个难题来自这里.
单线应该:
我正在寻找实用的技巧和窍门(补充例子perldoc perlrun).
我有一个在100维空间中有500,000个点的数据库,我想找到最接近的2个点.我该怎么做?
更新:太空是欧几里得,对不起.并感谢所有的答案.顺便说一句,这不是功课.
algorithm performance nearest-neighbor pca approximate-nn-searching
我正在尝试在Python中创建一个简单的Caesar Cipher函数,它根据用户的输入移动字母,并在最后创建一个最终的新字符串.唯一的问题是最终的密文只显示最后一个移位的字符,而不是一个包含所有移位字符的整个字符串.
这是我的代码:
plainText = raw_input("What is your plaintext? ")
shift = int(raw_input("What is your shift? "))
def caesar(plainText, shift):
for ch in plainText:
if ch.isalpha():
stayInAlphabet = ord(ch) + shift
if stayInAlphabet > ord('z'):
stayInAlphabet -= 26
finalLetter = chr(stayInAlphabet)
cipherText = ""
cipherText += finalLetter
print "Your ciphertext is: ", cipherText
return cipherText
caesar(plainText, shift)
Run Code Online (Sandbox Code Playgroud) 我有以下代码,我想更新到Python 3.x所需的库将更改为http.client和json.
我似乎无法理解如何做到这一点.你能帮忙吗?
import urllib2
import json
data = {"text": "Hello world github/linguist#1 **cool**, and #1!"}
json_data = json.dumps(data)
req = urllib2.Request("https://api.github.com/markdown")
result = urllib2.urlopen(req, json_data)
print '\n'.join(result.readlines())
Run Code Online (Sandbox Code Playgroud) 我有一个字典,我想动态插入键和值,但我没有设法做到这一点.问题是,当我使用的更新方法不增加一对,但这样我只有最后一个值的时候,这里印刷的字典是我的代码将删除以前的值
i = 0
for o in iterload(f):
i=i+1
mydic = {i : o["name"]}
mydic.update({i : o["name"]})
for k, v in mydic.items():
print(k,v)
print(mydic)
Run Code Online (Sandbox Code Playgroud)
f是我正在使用python代码解析的文件,因为我得到了
{3: 'toto'}
Run Code Online (Sandbox Code Playgroud)
这是最后一个元素.有没有解决方案,我的词典中包含所有元素
提前致谢
我有另一个问题
现在我需要知道输入值是否等于我的字典中的键,如果是,我需要获取此键的值以继续解析文件并获取其他信息.
这是我的代码:
f = open('myfile','r')
nb_name = input("\nChoose the number of the name :")
for o in iterload(f):
if o["name"] == mydic[nb_name]:
...
Run Code Online (Sandbox Code Playgroud)
我得到一个keyError
Traceback (most recent call last):
File ".../test.py", line 37, in <module>
if o["name"] == mydic[nb_name]:
KeyError: '1'
Run Code Online (Sandbox Code Playgroud)
我不明白这个问题
例如,存储一百万(32位)整数列表需要多少内存?
alist = range(1000000) # or list(range(1000000)) in Python 3.0
Run Code Online (Sandbox Code Playgroud) 实现命令行UI的"最干净"方式是什么,类似于git,例如:
git push origin/master
git remote add origin git://example.com master
Run Code Online (Sandbox Code Playgroud)
理想情况下还允许更灵活的解析,例如,
jump_to_folder app theappname v2
jump_to_folder app theappname source
jump_to_folder app theappname source v2
jump_to_folder app theappname build v1
jump_to_folder app theappname build 1
jump_to_folder app theappname v2 build
Run Code Online (Sandbox Code Playgroud)
jump_to_folder是脚本名称,app是命令,theappname是"固定位置"参数,"构建"和"v2"等是参数(例如,可能的参数是任何数字/任何前缀为av的数字,或者build/source/TMP /配置)
我可以用一系列if/ else/ 手动解析参数elifs,但必须有更优雅的方法来做到这一点?
作为一个完全理论上的例子,我可以描述UI模式..
app:
fixed: application_name
optional params:
arg subsection:
"build"
"source"
"tmp"
"config"
arg version:
integer
"v" + integer
Run Code Online (Sandbox Code Playgroud)
然后通过上面的模式解析提供的参数,并获取一个字典:
>>> print schema.parse(["app", "theappname", "v1", "source"])
{ …Run Code Online (Sandbox Code Playgroud) 我一直想知道,游戏程序员如何将外部3D建模软件(如maya或3d max)中完成的游戏角色与用最喜欢的编程语言(如c或c ++)完成的实际游戏逻辑结合在一起.
你如何将这两件事结合在一起,从建模角色到编程,构建游戏的实际过程是什么?
让我想知道的一些事情是,你是从代码还是从3D模型编写角色动作?
例子真的很高兴看到.
以下指定文件的字符编码:
# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
但是大写的变体:
# -*- coding: UTF-8 -*-
Run Code Online (Sandbox Code Playgroud)
生产:
警告(骡子):编码系统无效`UTF-8'由当前缓冲区/文件指定:coding标签.强烈建议在写入文件之前修复它.
版: GNU Emacs 23.3.1
add-to-coding-system-list在这种情况下,显而易见的功能不适用.如何在声明中使UTF-8成为utf-8的别名coding?
python ×5
algorithm ×2
python-3.x ×2
3d ×1
command-line ×1
dictionary ×1
emacs ×1
key ×1
pca ×1
performance ×1
perl ×1
puzzle ×1
reverse ×1
shell ×1
standards ×1