[Kata 7 C++]Shortest Word

描述

Simple, given a string of words, return the length of the shortest word(s).

String will never be empty and you do not need to account for different data types.

测试用例

#include <iostream>
#include <string>
#include <vector>
#include <random>

Describe(Tests)
{
  It(Sample_Test_Cases)
  {
    Assert::That(find_short("bitcoin take over the world maybe who knows perhaps"), Equals(3));
    Assert::That(find_short("turns out random test cases are easier than writing out basic ones"), Equals(3));
    Assert::That(find_short("lets talk about javascript the best language"), Equals(3));
    Assert::That(find_short("i want to travel the world writing code one day"), Equals(1));
    Assert::That(find_short("Lets all go on holiday somewhere very cold"), Equals(2));
    Assert::That(find_short("Let's travel abroad shall we"), Equals(2));
  }
  It(Random_Test_Cases)
  {
    for(int i = 0; i < 200; i++)
    {
      std::string str = make_string(rand_number(1, 20));
      Assert::That(find_short(str), Equals(sol(str)));
    }
  }
  std::string make_string(int length)
  {
    std::string str;
    for (int i = 1; i < length; i++)
    {
      str += make_word(1, 20);
    }
    return str;
  }
  std::string make_word(int min, int max)
  {
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_real_distribution<> dis(0.0, 1.0);
  
    std::string possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    int length = ceil((dis(gen) * max) + min);
    std::string word = "";
    
    for(int i = 0; i < length; i++)
      word += possible[floor(dis(gen) * possible.size())];
      
    return word;
  }
  int rand_number(int min, int max)
  {
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_real_distribution<> dis(0.0, 1.0);
    return ceil((dis(gen) * max) + min);
  }
  int sol(std::string str)
  {
    std::vector<std::string> array;
    std::stringstream ss(str);
    std::string x;
    while (ss >> x) {
      array.push_back(x);
    }
    int length = array.size();
    unsigned long min = 1000;
    for (int i = 0; i < length; i++)
    {
      if (array[i].length() < min)
      {
        min = array[i].length();
      }
    }
    return min;
  }
};

解决方案

istringstream

int find_short(const std::string &str)
{
    std::istringstream inp(str);
    std::string s;
    int len = -1;
    while (std::getline(inp, s, ' '))
        if (s.length() < len)
            len = s.length();
    return len;
}
int find_short(std::string str){
  std::stringstream oh_god_yes(str);
  std::string word;
  int min = 0;
  while (oh_god_yes >> word) if (word.length() < min || min == 0) min = word.length();
  return min;
}
//使用 >> 赋值

#include “sstream”
istringstream 将句子分为单词,默认分割符是空格或者回车符或者tab

   istringstream is("I am\tlove C++\nand I love C");
    string str;
    //默认是空格或者回车符或者tab,会输出如下内容:I am love C++ and I love C
    while (is>>str){
        cout<<str<<" ";
    }
    cout<<endl;
    istringstream istringstream1("30+2*2+3-");
    long ans;
    char op;
    istringstream1>>ans;
    //会得到如下内容 30 + 2 * 2 + 3 -
    while (istringstream1 >> op) {
        cout<<ans<<" "<<op<<" ";
        istringstream1>>ans;
    }
    cout<<endl;

    istringstream istringstream2("20q230r4");
    istringstream2>>ans;
    //会得到如下内容  20 q 230 r
    while (istringstream2>>op){
        cout<<ans<<" "<<op<<" ";
        istringstream2>>ans;
    }

遍历

#include <cctype>
#include <string>

using str_size = std::string::size_type;

auto find_short(const std::string& str) -> str_size
{
  str_size min_len{str.length()};  //最小长度设为句子长度
  str_size current_len{0};
  
  for (auto c : str)
  {
    if (::isspace(c))
    {
      min_len = std::min(min_len, current_len);
      current_len = 0;
    }
    else
    {
      current_len++;
    }
  }
  return std::min(min_len, current_len);  //使用min避免没有计算最后一个单词
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值