入坑codewars第二天-Basic Mathematical Operations、Ones and Zeros
一、題目
需要實現 +對應加法
需要實現 -對應減法
……
以此類推
二、求解
簡單的if語句
二、題二
題意就是:將列表中的數字看成一串二進位制然後轉成十進位制數
我的思路是:先把列表顛倒,這裡注意列表a,a.reverse()就直接把a顛倒了;因為二進位制是從右到左讀,因此我先顛倒從左往右方便計算。
(1)先顛倒列表:
(2)迴圈用列表數字乘以2^n
(3)依次相加
程式碼如下:
def binary_array_to_number(arr):
arr.reverse()
sum1=0
i=0
for x in arr:
sum1=sum1+(2**i)*x
i=i+1
return sum1
相關推薦
入坑codewars第二天-Basic Mathematical Operations、Ones and Zeros
一、題目 需要實現 +對應加法 需要實現 -對應減法 …… 以此類推 二、求解 簡單的if語句 二、題二 題意就是:將列表中的數字看成一串二進位制然後轉成十進位制數 我的思路是:先把列表顛倒,這裡注意列表a,a.rev
入坑codewars第一天-Invert values、String repeat、List Filtering
今天導師給開了會讓我們每天在codewars上至少刷兩道題,提高自己的python程式設計能力。不刷不知道一刷嚇一跳。果然自己的程式設計能力很菜。實踐出真知。 一、題目 題目說明:就是將列表中所有的取反,題目很簡單,但是我不太會運用python程式碼 二、求解 就是一個fo
入坑codewars第四天-Delete occurrences of an element if it occurs more than n times
第一題: Enough is enough! Alice and Bob were on a holiday. Both of them took many pictures of the places they've been, and now they want to show Char
入坑codewars第三天-Build a pile of Cubes、Convert boolean values to strings 'Yes' or 'No'、Build Tower
第一題: Build a pile of Cubes: Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of n
入坑codewars第18天-Alphabet wars - nuclear strike
題目:Alphabet wars - nuclear strike Introduction There is a war and nobody knows - the alphabet war! The letters hide in their nuclear shelters. T
入坑codewars第17天-Directions Reduction
題目: Once upon a time, on a way through the old wild west,… … a man was given directions to go from one point to another. The directions were "NO
入坑codewars第16天-Transformation of a Number Through Prime Factorization
題目: Every natural number, n, may have a prime factorization like: We define the geometric derivative of n, as a number with the fol
入坑codewars第15天-Regexp Basics - is it IPv4 address?
題目: Implement String#ipv4_address?, which should return true if given object is an IPv4 address - four numbers (0-255) separated by dots. I
入坑codewars第14天-Where is my parent!?(cry)
題目: Mothers arranged dance party for children in school.On that party there are only mothers and their children.All are having great fun on dancin
入坑codewars第13天- Dashatize it
第一題: Given a number, return a string with dash'-'marks before and after each odd integer, but do not begin or end the string with a dash mark. E
入坑codewars第12天-Best travel
題目一: John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. ls =
入坑codewars第11天-Clock in Mirror
題目一: Peter can see a clock in the mirror from the place he sits in the office. When he saw the clock shows 12:22 He knows that the time is 11:38
入坑codewars第十天-Product of consecutive Fib numbers、longest_palindrome
題目: The Fibonacci numbers are the numbers in the following integer sequence (Fn): 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
入坑codewars第八天-Help the bookseller !
今天只寫了一道題不容易呀,實在不懂輸出格式,於是想辦法湊出來的…… 題目: A bookseller has lots of books classified in 26 categories labeled A, B, ... Z. Each book has a code c of
入坑codewars第七天-Maximum subarray sum、Are they the “same”?
題目: 題意:求連續子序列的和的最大值 思路:笨方法一個一個求:首先求第一個數分別加後面的數取一個最大值 然後下一個數分別加後面的數取最大值 以此類推…… 程式碼如下: def maxSequence(arr): maxsum=0
入坑codewars第六天-Highest Scoring Word、The Supermarket Queue
題目: Given a string of words, you need to find the highest scoring word. Each letter of a word scores points according to it's position in the al
入坑codewars第五天-Dubstep、Regex validate PIN code
第一題: 題目: Polycarpus works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to
vue的一些坑(第二天)
作用 小童 過濾 文本 click rip col 官方 小數點 首先啊感謝一位小童鞋的指出其實我寫的這些內容算不上坑,只是平時使用的時候的一點小問題,不過只是個名字啦!再次致謝 1:兩個簡寫的命令 v-bind:class可以簡寫為 :class v-on:click可
入坑codewars第九天-Title Case
題目: Title Case A string is considered to be in title case if each word in the string is either (a) capitalised (that is, only the first letter o
MySQL第二天--where條件查詢、檢視及函式
//把資料庫匯出到指令碼檔案 mysqldump -uroot -p1234 --databases abc > d:/a/abc.sql //--databases是需要兩個-- Where