【PHP】单词十以内的加法de多种解法

简介: 问题描述把两个英文单词加在一起~写一个类Arith,实现下面的功能 var k = new Arith("three"); k.add("seven"); //返回 "ten"输入 - zero 到 ten ,肯定是小写 输出 - 返回相加的结果,肯...

问题描述

把两个英文单词加在一起~

写一个类Arith,实现下面的功能

  var k = new Arith("three");
  k.add("seven"); //返回 "ten"

输入 - zero 到 ten ,肯定是小写
输出 - 返回相加的结果,肯定是小写

分析

  • class
  • __construct
  • array
  • switch case

解决方案

数组取值array_flip反转键值


class Arith { 
    private $number;

    public $numbers = [
      'zero' => 0, 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5,
      'six' => 6, 'seven' => 7, 'eight' => 8, 'nine' => 9, 'ten' => 10, 'eleven' => 11,
      'twelve' => 12, 'thirteen' => 13, 'fourteen' => 14, 'fifteen' => 15, 'sixteen' => 16,
      'seventeen' => 17, 'eighteen' => 18, 'nineteen' => 19, 'twenty' => 20
    ];

    public function __construct(string $number) {
      $this->number = $number;
    }

    public function add(string $number): string {
      $sum = $this->get($this->number) + $this->get($number);
      $flipped = array_flip($this->numbers);

      return $flipped[$sum];
    }

    public function get(string $number): int {
      // can convert to lower
      return (empty($this->numbers[$number])) ? 0 : $this->numbers[$number];
    }
}

const定义关系array_search查找对应关系

class Arith{ 
    private $i;
    const A = [
      'zero' => 0,
      'one' => 1,
      'two' => 2,
      'three' => 3,
      'four' => 4,
      'five' => 5,
      'six' => 6,
      'seven' => 7,
      'eight' => 8,
      'nine' => 9,
      'ten' => 10,
      'eleven' => 11,
      'twelve' => 12,
      'thirteen' => 13,
      'fourteen' => 14,
      'fifteen' => 15,
      'sixteen' => 16,
      'seventeen' => 17,
      'eighteen' => 18,
      'nineteen' => 19,
      'twenty' => 20
    ];

    public function __construct(string $i)
    {
      $this->i = self::A[$i];
    }

    public function add(string $v) {
        return array_search($this->i + self::A[$v], self::A);
    }
}

数组的索引查找关系


class Arith{ 
    private $_numbers = ["zero" => 0, "one" => 1, "two" => 2, "three" => 3, 
      "four" => 4, "five" => 5, "six" => 6, "seven" => 7, "eight" => 8,
      "nine" => 9, "ten" => 10, "eleven" => 11];

    private $_words = ["zero", "one", "two", "three", 
      "four", "five", "six", "seven", "eight",
      "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
      "sixteen", "seventeen", "eighteen", "nineteen", "twenty"];  

    private $_word = '';

    public function __construct($word) {
      $this->_word = $word;
    }

    public function add($word) {
      return $this->_words[$this->_numbers[$this->_word] + $this->_numbers[$word]];
    }
}

switch case确立关系

class Arith
{

    protected $num;

    //初始化数据

    public function __construct($num)
    {
        $this->num = $this->_tonNum($num);

    }

    private function _tonNum($num)
    {
        $num_num = 0;
        switch ($num) {
            case "zero":
                $num_num = 0;
                break;
            case "one":
                $num_num = 1;
                break;
            case "two":
                $num_num = 2;
                break;
            case "three":
                $num_num = 3;
                break;
            case "four":
                $num_num = 4;
                break;
            case "five":
                $num_num = 5;
                break;
            case "six":
                $num_num = 6;
                break;
            case "seven":
                $num_num = 7;
                break;
            case "eight":
                $num_num = 8;
                break;
            case "nine":
                $num_num = 9;
                break;
            case "ten":
                $num_num = 10;
                break;
            case "eleven":
                $num_num = 11;
                break;
            case "twelve":
                $num_num = 12;
                break;
            case "thirteen":
                $num_num = 13;
                break;
            case "fourteen":
                $num_num = 14;
                break;
            case "fifteen":
                $num_num = 15;
                break;
            case "sixteen":
                $num_num = 16;
                break;
            case "seventeen":
                $num_num = 17;
                break;
            case "eighteen":
                $num_num = 18;
                break;
            case "nineteen":
                $num_num = 19;
                break;
            case "twenty":
                $num_num = 20;
                break;
        }
        return $num_num;
    }


    /**
     * 转换成文字
     * @param $num
     */
    public function tostr($num)
    {
        $num_num = "";
        switch ($num) {
            case 0:
                $num_num = "zero";
                break;
            case 1:
                $num_num = "one";
                break;
            case 2:
                $num_num = "two";
                break;
            case 3:
                $num_num = "three";
                break;
            case 4:
                $num_num = "four";
                break;
            case 5:
                $num_num = "five";
                break;
            case 6:
                $num_num = "six";
                break;
            case 7:
                $num_num = "seven";
                break;
            case 8:
                $num_num = "eight";
                break;
            case 9:
                $num_num = "nine";
                break;
            case 10:
                $num_num = "ten";
                break;
            case 11:
                $num_num = "eleven";
                break;
            case 12:
                $num_num = "twelve";
                break;
            case 13:
                $num_num = "thirteen";
                break;
            case 14:
                $num_num = "fourteen";
                break;
            case 15:
                $num_num = "fifteen";
                break;
            case 16:
                $num_num = "sixteen";
                break;
            case 17:
                $num_num = "seventeen";
                break;
            case 18:
                $num_num = "eighteen";
                break;
            case 19:
                $num_num = "nineteen";
                break;
            case 20:
                $num_num = "twenty";
                break;
        }
        return $num_num;
    }

    //增加的方法
    public function add($add_num)
    {
        //做加法
        $add_num = $this->_tonNum($add_num);
        $re = $this->tostr($add_num + $this->num);
        //转换
        return $re;
    }

}

总结

解决问题的整体思路是差不多的,但是在具体实现细节上会有区别,而具体实现的点也就是考察的价值。

目录
相关文章
|
6月前
|
SQL PHP 数据安全/隐私保护
拿php写一个投票系统出来,比较简单de啦
拿php写一个投票系统出来,比较简单de啦
拿php写一个投票系统出来,比较简单de啦
PHP:实现每个单词首字母大写
PHP:实现每个单词首字母大写
|
PHP
【PHP】字符串去空格并将每个单词首字母转换成大写de多种解法
问题描述 编写camel_case方法,实现将字符串去掉空格,并将每个单词的第一个字母转换成大写。 例如: camel_case("hello case"); // => "HelloCase" camel_case("camel case word"); // =>...
1367 0
|
PHP
【PHP】给两个字符串返回字符串按排序出两个字符串的字符de多种解法
问题描述 给出两个字符串s1和s2,两个字符串仅包含a-z,返回一个排序后的字符串,包含这两个字符串的内容,每个字母只出现一次。 举例: a = "xyaabbbccccdefww" b = "xxxxyyyyabklmopq" longest(a, b) -> "...
1164 0
|
2月前
|
安全 关系型数据库 MySQL
PHP与MySQL交互:从入门到实践
【9月更文挑战第20天】在数字时代的浪潮中,掌握PHP与MySQL的互动成为了开发动态网站和应用程序的关键。本文将通过简明的语言和实例,引导你理解PHP如何与MySQL数据库进行对话,开启你的编程之旅。我们将从连接数据库开始,逐步深入到执行查询、处理结果,以及应对常见的挑战。无论你是初学者还是希望提升技能的开发者,这篇文章都将为你提供实用的知识和技巧。让我们一起探索PHP与MySQL交互的世界,解锁数据的力量!
|
2月前
|
NoSQL 关系型数据库 MySQL
不是 PHP 不行了,而是 MySQL 数据库扛不住啊
【9月更文挑战第8天】这段内容讨论了MySQL在某些场景下面临的挑战及其原因,并指出这些问题不能完全归咎于MySQL本身。高并发读写压力、数据量增长以及复杂查询和事务处理都可能导致性能瓶颈。然而,应用程序设计不合理、系统架构不佳以及其他数据库选择和优化策略不足也是重要因素。综合考虑这些方面才能有效解决性能问题,而MySQL通过不断改进和优化,仍然是许多应用场景中的可靠选择。
140 9