codewars解题笔记---The Feast of Many Beasts

Instructions:

All of the animals are having a feast! Each animal is bringing one dish. There is just one rule: the dish must start and end with the same letters as the animal's name. For example, the great blue heron is bringing garlic naan and the chickadee is bringing chocolate cake.

Write a function feast that takes the animal's name and dish as arguments and returns true or false to indicate whether the beast is allowed to bring the dish to the feast.

Assume that beast and dish are always lowercase strings, and that each has at least two letters. beast and dish may contain hyphens and spaces, but these will not appear at the beginning or end of the string. They will not contain numerals.

Sample Tests:

import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runners.JUnit4;

// TODO: Replace examples and use TDD development by writing your own tests

public class SolutionTest {
    @Test
    public void fixedTest() {
         assertTrue(Kata.feast("great blue heron","garlic nann"));
         assertTrue(Kata.feast("chickadee","chocolate cake"));
         assertFalse(Kata.feast("brown bear","bear claw"));
    }
}

Solution:

public class Kata {

  public static boolean feast(String beast, String dish) {
    
    return false;
    
  }
}
  

题目的解释:

所有的动物都在享用盛宴!每只动物都带来一道菜。只有一条规则:菜的开头和结尾必须与动物的名字相同。例如,大蓝鹭带来了大蒜,而山雀带来了巧克力蛋糕。
写一个函数盛宴,以动物的名字和盘子作为论据,返回“真”或“假”,表明是否允许野兽把盘子带到盛宴上。
假设beast和dish总是小写字符串,并且每个字符串至少有两个字母。Beast和Dish可能包含连字符和空格,但这些不会出现在字符串的开头或结尾。它们不包含数字。(俗话就是两个字符串的首尾的字母一致的话就返回true)

我的答案:

public class Kata {

  public static boolean feast(String beast, String dish) {
       String shou = beast.substring(0,1);
       String shou2 = dish.substring(0,1);
       String wei = beast.substring(beast.length()-1,beast.length());
       String wei2 = dish.substring(dish.length()-1,dish.length());
       if(shou.equals(shou2) && wei.equals(wei2)){
           return true;
       }else{
           return false;
       }
  }
}

最优答案:

public class Kata {

  public static boolean feast(String beast, String dish) {
    
    return beast.charAt(0)==dish.charAt(0) && beast.charAt(beast.length()-1) == dish.charAt(dish.length()-1);
    
  }
  
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pythonfeast是一个用于特征存储和管理的开源库。下面是一个使用feast的示例: 首先,我们需要准备一些特征数据。假设我们正在开发一个电商推荐系统,我们想要使用用户的浏览历史和购买记录作为特征。我们可以定义一个“user_features.csv”文件,其中包含用户的ID、浏览历史和购买记录。类似地,我们可以定义一个“product_features.csv”文件,其中包含产品的ID、价格和品类等特征。 接下来,我们可以使用feast来创建一个特征实体。我们可以使用命令行工具或编程接口,使用以下代码创建一个新的特征实体: ```python import feast # 创建一个feast客户端 client = feast.Client() # 创建一个特征实体 client.apply_entity_from_csv(entity="user", entity_source="user_features.csv", schema="user_id:int64, view_count:int64, purchase_count:int64") client.apply_entity_from_csv(entity="product", entity_source="product_features.csv", schema="product_id:int64, price:double, category:string") ``` 我们定义了两个特征实体,一个是用户(user),一个是产品(product)。通过定义它们的名称、来源和模式,我们可以让feast知道如何加载和管理这些特征。 接下来,我们可以定义一个特征集(FeatureSet)。特征集是一个包含多个特征的集合,可以用来训练模型或进行推断。以下是一个使用feast定义特征集的示例: ```python # 创建一个特征集 client.apply_feature_set(feature_set="user_view_stats", features=["user.view_count", "user.purchase_count", "product.price"], entities=["user", "product"], batch_source="user_features.csv", stream_source="user_clickstream", schema="user_id:int64, view_count:int64, purchase_count:int64, price:double") ``` 在上述示例中,我们定义了一个名为“user_view_stats”的特征集。它包含了用户的浏览次数、购买次数以及产品的价格特征。我们还指定了特征集的实体(即用户和产品),以及特征集的数据来源(批处理和流处理)。 通过以上步骤,我们成功地创建了特征实体和特征集,并使用feast进行特征管理和存储。在实际使用feast时,还可以通过feast的查询接口获取特征数据,并进行模型训练或推断等操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值