我希望这个要求是合法的.我正在为工程师学习python的编程课程,所以我对这个行业有点新意.无论如何,在我的作业中,我被要求编写一个函数,接收两个字符串,并检查一个是另一个的(置换/ Anagrm).(这意味着如果它们的每个字母都有完全相同的字母和相同数量的外观)
我在搜索时发现了一些很棒的代码,但我仍然没有弄清楚我的代码有什么问题(对我来说,了解我的学习过程很重要).
我们有一个测试文件,假设检查我们的功能,它给了我这个错误:
Traceback (most recent call last):
File "C:\Users\Or\Desktop\?????\4\hw4\123456789_a4.py", line 110, in <module>
test_hw4()
File "C:\Users\Or\Desktop\?????\4\hw4\123456789_a4.py", line 97, in test_hw4
test(is_anagram('Tom Marvolo Riddle','I Am Lord Voldemort'), True)
File "C:\Users\Or\Desktop\?????\4\hw4\123456789_a4.py", line 31, in is_anagram
s2_list.sort()
NameError: global name 's2_list' is not defined
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
def is_anagram(string1, string2):
string1 = string1.lower() #turns Capital letter to small ones
string2 = string2.lower()
string1 = string1.replace(" ","") #turns the words inside the string to one word
string2 = string2.replace(" ","")
if len(string1)!= len(string2):
return …Run Code Online (Sandbox Code Playgroud) 我想确保我只打印最多80个字符的长行,但我有一个字符串s,可以更短,更长.所以我想把它分成几行而不分裂任何单词.
长字符串示例:
s = "This is a long string that is holding more than 80 characters and thus should be split into several lines. That is if everything is working properly and nicely and all that. No mishaps no typos. No bugs. But I want the code too look good too. That's the problem!"
Run Code Online (Sandbox Code Playgroud)
我可以设计出这样做的方法,例如:
words = s.split(" ")
line = ""
for w in words:
if len(line) + len(w) <= 80:
line += "%s " …Run Code Online (Sandbox Code Playgroud) 我想计算字符串中的单词数。就像如果给定一个字符串:
string str = "Hello! How are you?";
Run Code Online (Sandbox Code Playgroud)
那么输出将是:
Number of words in string “Hello! How are you?” is 4.
Run Code Online (Sandbox Code Playgroud)
我正在使用 for 循环,这些是我当前的代码。
string wordCountStr = "";
int noOfWords = 0;
private void btn_Computate4_Click(object sender, EventArgs e)
{
wordCountStr = tb_Qns4Input.Text.ToString(); //tb_Qns4Input is a textbox.
for (int i = 0; i< wordCountStr.Length; i++)
{
//I don't know how to code here.
}
lbl_ResultQns4.Text = "Number of words in string " + wordCountStr + " is " + noOfWords;
}
Run Code Online (Sandbox Code Playgroud)
哦,是的,我正在使用 …
我有这个数组:
char alph[] ={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9','.',',',':','?','=','-','(',')','"','\'','/','%','@','!'};
Run Code Online (Sandbox Code Playgroud)
可以用'perc'之类的单词更改符号'%'吗?
谢谢您的帮助!
我有一个像这样的字符串how are you,我希望得到所有可能的字样
how are you
are how you
you how are
you are how
are you how
how you are
Run Code Online (Sandbox Code Playgroud)
如何在perl脚本中创建它,我已经尝试了该shuffle函数,但它只返回一个shuffle字符串.
如果您不熟悉Perl脚本,则只能告诉我逻辑.
注意:字符串中的字数不是常量.
我有以下句子:
This is a text and we should print each word
Run Code Online (Sandbox Code Playgroud)
我想打印这句话中的每个单词.
package lab2_3;
public class Main {
public static void main(String[] args) {
String s2 = "This is a text and we should print each word";
int i;
int j;
for (i = 0; i <= s2.length() - 1; i++){
if (s2.substring(i).startsWith(" ") || i == 0){
//here I search for the start of the sentence or " "
for (j = i + 1; j <= s2.length() - …Run Code Online (Sandbox Code Playgroud) 让我们举一个英语句子的例子:
string sentence = "Hello, How are you?";
Run Code Online (Sandbox Code Playgroud)
因此,如果我想搜索单词“ you”,则可以使用:
if (sentence.Contains("you")); // This would be true
Run Code Online (Sandbox Code Playgroud)
但是如果我搜索这个:
if (sentence.Contains("ello")); // This would also be true
Run Code Online (Sandbox Code Playgroud)
但我希望它是错误的。我想搜索整个单词。不是文字的一部分
如何在C#中执行此操作?
我希望能够计算文本文件中给定单词(例如输入)的出现次数。我有这个代码,它给了我文件中所有单词的出现:
word_count = {}
my_word = id
File.open("texte.txt", "r") do |f|
f.each_line do |line|
words = line.split(' ').each do |word|
word_count[word] += 1 if word_count.has_key? my_word
word_count[word] = 1 if not word_count.has_key? my_word
end
end
end
puts "\n"+ word_count.to_s
Run Code Online (Sandbox Code Playgroud)
谢谢你
我看过这个链接计算机中的字大小是什么意思?.它定义了字大小.我试图用比特表示非常长的字符串,其中每个字符由4位表示并保存在长整数或整数数组中,以便我可以在需要时提取我的字符串.我可以将这些位保存为整数数组或长数组.
- 如果我使用长数组(8字节),我将能够在一个长数组中保存8*4 = 32位.
- 但是如果我使用int,我将只能保存4*4 = 16位.
现在,如果我的Word大小= 32,那么我应该只使用int而不是长.