0047【Edabit ★☆☆☆☆☆】Minimal I: If Boolean Then Boolean

2023-10-28 19:52
文章标签 boolean minimal edabit 0047

本文主要是介绍0047【Edabit ★☆☆☆☆☆】Minimal I: If Boolean Then Boolean,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

0047【Edabit ★☆☆☆☆☆】Minimal I: If Boolean Then Boolean

conditions language_fundamentals logic validation

Instructions

In this series we’re going to see common redundancies and superfluities that make our code unnecessarily complicated and less readable, and we’re going to learn how to avoid them.

In line with the spirit of the series, we can summarize the general rules of minimalist code in two simple principles:

  • Keep your code clean and readable.
  • While not violating the first principle: get rid of everything superfluous.

In order to achieve this you should:

  • Deepen your knowledge of logics.
  • Deepen your understanding of the particular language you’re coding with.

I would also add: observe and learn from the pros. Make a habit of checking the Solutions tab after solving a challenge on Edabit. There is absolutely nothing wrong in assimilating features of someone else’s coding style, especially if yours is not yet fully developed.

Goal

In the Code tab you will find a code that is missing a single character in order to pass the tests. However, YOUR GOAL is to submit a function as minimalist as possible. Use the tips in the Tips section down below.

Write a function that returns true if the given integer is even, and false if it’s odd.

Tips

Using an if statement in order to return boolean or to set a variable to a boolean is redundant.

A function that returns true if a person’s age is 18 or greater and false otherwise, could be written as:

function legalAge(age) {if(age >= 18) {return true}else {return false}
}

Notice that age >= 18 will already give us a boolean (true or false). This means that the function can be written in a much simpler and cleaner way:

function legalAge(age) {return age >= 18
}
Examples
function legalAge(age) {if(age >= 18) {return true}else {return false}
}
Notes
  • his is an open series: there isn’t a definite list of features for the challenges.
Solutions
// issue code
function isEven(n) {if n % 2 === 0 {return true}else if n % 2 === 1 {return false}
}
// correct it !!
function isEven(n) {return (n % 2)==0
}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}},assertSimilar:function(actual,expected){if(actual.length != expected.length){throw new Error(`length is not equals, ${actual},${expected}`);}for(let a of actual){if(!expected.includes(a)){throw new Error(`missing ${a}`);}}}}
})();
Test.assertEquals(isEven(2), true)
Test.assertEquals(isEven(3), false)
Test.assertEquals(isEven(10), true)
Test.assertEquals(isEven(31), false)
Test.assertEquals(isEven(666), true)
Test.assertEquals(isEven(777), false)
Test.assertEquals(isEven(3482034), true)
Test.assertEquals(isEven(3482035), false)

这篇关于0047【Edabit ★☆☆☆☆☆】Minimal I: If Boolean Then Boolean的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/295573

相关文章

Minimal coverage -uva 覆盖线段,贪心

一道经典的贪心问题,具体方法就是将(an,bn)区间,按照an从小到大的顺序进行排序,之后从0开始, 取最大的有效区间,这里用到了结构体的快排,否则可能会超时. #include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX_SIZE 100000 + 10#define BOTTOM -50000 - 10str

【JavaScript】基本数据类型与引用数据类型区别(及为什么String、Boolean、Number基本数据类型会有属性和方法?)

基本数据类型   JavaScript基本数据类型包括:undefined、null、number、boolean、string。基本数据类型是按值访问的,就是说我们可以操作保存在变量中的实际的值。 1)基本数据类型的值是不可变的 任何方法都无法改变一个基本类型的值,比如一个字符串: var name = "change";name.substr();//hangconsole.log

BiometricPrompt.Builder.setDeviceCredentialAllowed(boolean)方法过时了,怎么破。

Android R中已经将该API setDeviceCredentialAllowed() 标记为@Deprecated,即API=30开始不推荐使用该API。 先来看看官方API怎么说? 明确说,请使用setAllowedAuthenticators(int)代替。 怎么用,上图API已经说的很清晰,举个栗子 int authenticators = BiometricMana

个人的 minimal-mistakes 配置记录

记录一下个人的 minimal-mistakes 配置 Modifty Change font size assets\css\main.scss html {font-size: 12px; // change to whatever@include breakpoint($medium) {font-size: 14px; // change to whatever}@include

ural Minimal Coverage (区间覆盖)

http://acm.timus.ru/problem.aspx?space=1&num=1303 给出一些区间,选择尽量少的区间能覆盖到[0,m]。 小白p154,典型的区间覆盖问题。一直在想怎么dp。。 首先预处理,先按左端点从小到大排序,若左端点相同右端点从大到小排序,若区间x完全包含y,按照贪心的思想,y是没有意义的,有大区间可以选何必选择小区间。处理完事之后各个区间满足a1

el-checkbox 状态切换,将boolean转换成1遇到的问题

项目场景: 项目中有一个需求,通过el-checkbox切换来控制控件的显示和隐藏,同时,切换的状态要上传后台,true为1.false为0 问题描述: 通过v-model去实现业务场景,结果没有效果, 原因分析: v-model一旦绑定过后,数据类型无法改变,所以无法将boolean转换为1或0 解决方案: 之后通过@change监听时间来实现 handleCheckAll

eslint工具编程“ Unnecessary use of boolean literals in conditional expression” 错误的解决方案

今天快下班提交代码时碰到这个很诡异的问题,如下图 这是我的代码报错的地方 很明显是eslint工具检测出来的,我们必须用它的代码规范来编程。后来查了下eslint官方文档,对其解释是: 当存在更简单的替代方案时,不允许三元运算符(不需要 - 三元) 也就是说我这种写法会显得啰嗦 改变这两行代码的写法 就ok了 详细解释说明传送

java.lang解析Boolean类

Boolean类: public final class Booleanextends Objectimplements Serializable, Comparable<Boolean>    字段摘要static BooleanFALSE           对应基值 false 的 Boolean 对象。static BooleanTRUE           对应基值 true 的

uva10020 - Minimal coverage(区间覆盖)

题目:uva10020 - Minimal coverage(区间覆盖) 题目大意:给出一些线段,然后问怎样取能使得最少的线段覆盖区间[0, M]. 解题思路:先预处理掉那些和区间【0,M】不沾边的线段。                  将线段按照起点小的排序。                   接着遍历这些线段。首先先判断起点最小的点是否<=0,如果不满足这个说明它不能覆

【服务器运维】CentOS6 minimal 离线安装MySQL5.7

1.准备安装包(版本因人而异,所以下面的命令中版本省略,实际操作中用Tab自动补全就好了) cloog-ppl-0.15.7-1.2.el6.x86_64.rpmcpp-4.4.7-23.el6.x86_64.rpmgcc-4.4.7-23.el6.x86_64.rpmgcc-c++-4.4.7-23.el6.x86_64.rpmglibc-2.12-1.212.el6.x86_64.r