我的下一个项目是写一个刽子手游戏.我认为它可以帮助我刷新字符串并提交I/O.
目前,我一直在阅读一个字符串文件到列表中.我试图避免全局变量,所以有人能指出我正确的方向将这个(可能已损坏的)代码变成一个返回列表的函数吗?
(defun read-word-list ()
"Returns a list of words read in from a file."
(let ((word-list (make-array 0
:adjustable t
:fill-pointer 0)))
(with-open-file (stream #p"wordlist.txt")
(loop for line = (read-line stream)
while line
(push line word-list)))
(select-target-word word-list)))))
Run Code Online (Sandbox Code Playgroud) 我正在研究Project Euler,这次问题#4.这个脚本的要点是找到两个三位数字的最大回文产品.我认为解决起来相当简单,但我得到的答案太低了.更具体地说,我得到580085,答案是906609.
有人能告诉我这是不正确的吗?
#!/usr/bin/env python
# encoding: utf-8
"""
P4.py
Created by Andrew Levenson on 2010-06-29.
Copyright (c) 2010 __MyCompanyName__. All rights reserved.
"""
import sys
import os
def main():
for x in range(100, 1000):
for y in range(100, 1000):
z = str( x * y )
s = str( z[::-1] ) # Reverse z
if z == s:
t = z
print t
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud) 我这次试着简明扼要!我还在使用Project Euler,这次回到#2.我真正的问题是我对Ruby很糟糕.当我运行以下代码时
x = 1
y = 2
sum = 2
while x >= 4_000_000 do |x|
sum += y if y % 2 == 0
z = x + y
x = x ^ y # xor magic
y = x ^ y # xor magic
x = x ^ y # xor magic
y = z
end
p sum
Run Code Online (Sandbox Code Playgroud)
我的翻译推出了以下输出:
/Users/Andy/Documents/Programming/Ruby/ProjectEuler/P2.rb:4: syntax error, unexpected '|'
while x >= 4_000_000 do |x|
^
Run Code Online (Sandbox Code Playgroud)
我正在阅读为什么(Poignant)指南Ruby,我很确定我的管道语法是正确的.有人能指出我在这里做错了什么吗?我已经尝试过很多不同的方式搞砸了,而且我很快就出现了
当我通过调用执行以下Common Lisp程序时(play),我收到错误:Argument X is not a NUMBER: Guess
;;;; number-game.lisp
;;;;
;;;; Andrew Levenson
;;;; 10/25/2010
;;;;
;;;; Simple number guessing game. User has
;;;; five guesses to determine a number between
;;;; one and one hundred, inclusive (1-100).
;;; Set global variable for the target number:
(defparameter *target* nil)
;;; Set the iterator so we may check the number of guesses
(defparameter *number-of-guesses* 0)
;;; Welcome the user
(defun welcome-user ()
(format t "Welcome to the …Run Code Online (Sandbox Code Playgroud) 我真的是Haskell的绝对新手,所以我完全不知道如何调试我写的一些函数.当我打电话给shuntingYard ["3+4"]我回来时[],我想回来[34+].任何和所有的帮助将非常非常感谢.
import Char
isOperator :: Char -> Bool
isOperator x = elem x ['+','-','*','/','%','^','!','=','<','>']
associativityOf :: Char -> String
associativityOf x = if elem x ['+','-','*','/','%']
then "Left"
else "Right"
precedenceOf :: Char -> Int
precedenceOf x
| elem x "=<>" = 1
| elem x "+-" = 2
| elem x "*/%" = 3
| elem x "^!" = 4
| otherwise = 0
operatorActions :: [[Char]] -> [[Char]] -> [[Char]]
operatorActions …Run Code Online (Sandbox Code Playgroud) 我在Ruby中搞砸了一些.我有一个包含两个方法的类的文件和以下代码:
if __FILE__ == $0
seq = NumericSequence.new
puts "\n1. Fibonacci Sequence"
puts "\n2. Pascal\'s Triangle"
puts "\nEnter your selection: "
choice = gets
puts "\nExcellent choice."
choice = case
when 1
puts "\n\nHow many fibonacci numbers would you like? "
limit = gets.to_i
seq.fibo(limit) { |x| puts "Fibonacci number: #{x}\n" }
when 2
puts "\n\nHow many rows of Pascal's Triangle would you like?"
n = gets.to_i
(0..n).each {|num| seq.pascal_triangle_row(num) \
{|row| puts "#{row} "}; puts "\n"}
end
end
Run Code Online (Sandbox Code Playgroud)
为什么我运行代码并提供选项2,它仍然运行第一个案例?
我试图通过用PHP编写的网页将一些简单的用户数据添加到数据库中,但是下面的代码(更具体地说,第三行)打破了页面.我使用错误的MySQL功能吗?我很确定我的查询格式正确.
mysql_query("CREATE TABLE stats ( userAgent CHAR(20) )");
$userAgent = $_SERVER["HTTP_USER_AGENT"];
mysql_query("INSERT INTO stats VALUES ("$userAgent"));
Run Code Online (Sandbox Code Playgroud) 我正在为CS1做一个家庭作业,我几乎完成了它,但是我试图实现的一些功能的错误不断出现.赋值是使用链表的大整数的经典加法和减法.我的问题不在于程序的任何数学功能,而是在完成时使链接列表正确打印.我很确定大多数问题都存在于其中stripLeadingZeros(); 功能如下.
/*
* Function stripLeadingZeros
*
* @Parameter STRUCT** Integer
*
* Step through a linked list, recursively unlinking
* all leading zeros and making the first
* non-zero integer the head of the list.
*/
struct integer* stripLeadingZeros( struct integer *p )
{
// Are we at the end of the list?
if( p == NULL ) return NULL;
// Are we deleting the current node?
if( p->digit == 0 )
{
struct integer *pNext;
pNext …Run Code Online (Sandbox Code Playgroud) 使用 Gensim 创建 FastText 模型后,我想加载它,但遇到了似乎与回调相关的错误。
用于创建模型的代码是
TRAIN_EPOCHS = 30
WINDOW = 5
MIN_COUNT = 50
DIMS = 256
vocab_model = gensim.models.FastText(sentences=model_input,
size=DIMS,
window=WINDOW,
iter=TRAIN_EPOCHS,
workers=6,
min_count=MIN_COUNT,
callbacks=[EpochSaver("./ftchkpts/")])
vocab_model.save('ft_256_min_50_model_30eps')
Run Code Online (Sandbox Code Playgroud)
回调EpochSaver定义为
from gensim.models.callbacks import CallbackAny2Vec
class EpochSaver(CallbackAny2Vec):
'''Callback to save model after each epoch and show training parameters '''
def __init__(self, savedir):
self.savedir = savedir
self.epoch = 0
os.makedirs(self.savedir, exist_ok=True)
def on_epoch_end(self, model):
savepath = os.path.join(self.savedir, f"ft256_{self.epoch}e")
model.save(savepath)
print(f"Epoch saved: {self.epoch + 1}")
if os.path.isfile(os.path.join(self.savedir, f"ft256_{self.epoch-1}e")):
os.remove(os.path.join(self.savedir, f"ft256_{self.epoch-1}e"))
print("Previous model …Run Code Online (Sandbox Code Playgroud) common-lisp ×2
lisp ×2
python ×2
ruby ×2
c ×1
callback ×1
fasttext ×1
gensim ×1
haskell ×1
javascript ×1
jupyter-lab ×1
linked-list ×1
mysql ×1
palindrome ×1
php ×1
sbcl ×1
stdin ×1
syntax ×1