小编Dan*_*mov的帖子

同时替换R中的所有变音符(使用正则表达式)

我有德语文字,我想将所有变音符号(ä,Ä,ü,Ü,ö,Ö)替换为ae,oe,ue等。

我可以单独进行操作(通过将每个替换保存到一个新文件中):

gsub(pattern = '[ä]', replacement = "ae",text)
gsub(pattern = '[ü]', replacement = "ue",text)
gsub(pattern = '[ö]', replacement = "oe",text)
Run Code Online (Sandbox Code Playgroud)

但是我可以在一个命令中做到这一点吗(包括用Ae,Oe和Ue等替换大写字母)?

我可以用正则表达式来做吗?

regex r

6
推荐指数
2
解决办法
3098
查看次数

C中的贪婪算法(带硬币)

在我的代码中,我使用了贪婪的算法,以便使用最少的硬币数量.例如:我必须返回0.41美元,我可以使用的最小硬币数量是4:

1 - 0,25;
1 - 0.10;
1 - 0.05;
1 - 0.01;
Run Code Online (Sandbox Code Playgroud)

有4种类型的硬币:0.25,0.10,0.05,0.01.

这是我的代码:

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    printf("Enter the sum, that you want to return you:");
    float sum = GetFloat();
    float quaters = 0.25;
    float dime = 0.10;
    float nickel = 0.05;
    float penny = 0.01;
    int count_q = 0,count_d = 0,count_n = 0,count_p = 0;


    while(sum<0){
        printf("Incorrect, enter the positive float number");
        sum = GetFloat();
    }

    while(sum > 0){
        if(sum - quaters >=0){
            sum …
Run Code Online (Sandbox Code Playgroud)

c if-statement greedy cs50

3
推荐指数
1
解决办法
483
查看次数

按空格拆分列表列表中的字符串

假设我有以下结构:

t = [['I will','take','care'],['I know','what','to','do']]
Run Code Online (Sandbox Code Playgroud)

正如你在第一个列表我有看到'I will',我希望他们分裂成两个元素'I''will',ST结果是:

[['I', 'will', 'take', 'care'], ['I', 'know', 'what', 'to', 'do']]
Run Code Online (Sandbox Code Playgroud)

快速和肮脏的算法如下:

train_text_new = []


for sent in t:
  new = []
  for word in sent:
    temp = word.split(' ')
    for item2 in temp:
      new.append(item2)


  train_text_new.append(new)
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有更易读且可能更有效的算法来解决这个问题。

python string list

3
推荐指数
1
解决办法
101
查看次数

将所有零移动到列表的末尾(找不到0.0的解决方案)

假设我有一个列表:[9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9,False]我想将所有零移到最后.

这是我的代码:

def move_zeros(array):
    new = [int(x) if x is 0.0 else x for x in array ]
    new.sort(key=lambda item: item is 0)
    return new 
Run Code Online (Sandbox Code Playgroud)

但是int(x)不能为0.0没有第二行而工作我无法移动0.0到最后.能帮我找到解决方案0.0吗?

EDITED

我通过eding False到列表更新了原始帖子

python sorting type-conversion

1
推荐指数
1
解决办法
424
查看次数

在C++中无法打破while循环

我创建了一个程序,计算棱镜的体积和矩形的面积.但我想让用户决定他想用这个程序多长时间:

#include <iostream>
using namespace std;

int main()
{
    bool run = true;
    while(run = true){
        double len,width,area,volume,height;
        char check;
        cout << "Please, enter length,width and height of a prism(rectangular)!";
        cin >> len >> width >> height;
        area = width*len;
        volume = area*height;
        cout << "The area of rectangle is equal to: " << area << "\n" << "The volume of rectangular prism is equal to: " << volume << "\n";
        cout << "Do you want to try again?(y/n)\n";
        cin …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
168
查看次数

标签 统计

python ×2

c ×1

c++ ×1

cs50 ×1

greedy ×1

if-statement ×1

list ×1

r ×1

regex ×1

sorting ×1

string ×1

type-conversion ×1