一些做着玩的题

这是我在平时有时间的时候做的一些算法上的题目

想看更新请移步这里

题目: Climbing Stairs

描述

You are climbing a stair case.It takes n steps to reach to the top

Each time you can either climb 1 or 2 steps. In how many distinct 
ways can you climb to the top?

Note: Given n will be a positive integer.

解法

这个问题当时拿到的时候是完全没有思路的,后面上网查询了一下这个题目,知道了使用
斐波那契数列就能够解这道题目,`F(0)=1,F(1)=1, F(n)=F(n-1)+F(n-2)(n>=2
,n∈N*)`当然百度作业帮上面也有相应的解法,套路就是

题目为一次可以走一步或两步:
a[1]=1;a[2]=2;
假设i的范围为3~n(n 为楼梯数),关系式为:
a[i]=a[i-1]+a[i-2];

若题目为一次可以走一步或两步或三步:
a[1]=1;a[2]=2;a[3]=4;
假设i的范围为4~n(n为楼梯数),关系式为:
a[i]=a[i-1]+a[i-2]+a[i-3]; 

但是当初看到这个数列的公式,第一想法是使用递归,然后,由于堆栈溢出,失败了
再看看后面的百度作业帮的那个公式,再联想一下数列,于是想到了使用数列这样的
结构来解决

/**
 * @param {number} n
 * @return {number}
 */
var climbStairs = function(n) {
    if (isNaN(n)) return -1;
    if (n<=0) return -1;
    if (n<=2) return n;
    if (n>=3) {
        var res = [1,2];
        for(var i=2;i<n;i++){  
            res[i] = res[i-1] + res[i-2];  
        }  
        return res[n-1];
    }
};

题目: Find the odd int

描述

Given an array, find the int that appears an odd number of times.

There will always be only one integer that appears an odd number
 of times.

解法

这是我的解法,写的真的很丑,勿喷哦

function findOdd(A) {
  //happy coding!
  let indecies = [];
  
  A.forEach(a => {
    let idx = A.indexOf(a);
    let index = [];
    while (idx !== -1) {
      index.push(idx);
      idx = A.indexOf(a, idx + 1);
    }
    indecies.push(index);
  });
  const a = indecies.filter(index => (index.length % 2 !== 0));
  return A[a[0][0]];
}

然后是大神的解法,使用的是位运算(说实话,还不是很懂位运算,如果有什么见解,欢迎留言)

function findOdd(A) {
  //happy coding!
  return A.reduce((a, b) => a^b);
}

题目:You're a square!

描述

Given an integral number, determine if it's a square number:
isSquare(-1) => false
isSquare( 3) => false
isSquare( 4) => true
isSquare(25) => true
isSquare(26) => false

解法

这道就简单了

var isSquare = function(n){
  return Number.isInteger(Math.sqrt(n));
}

题目:Strings to numbers

描述

You are given an array of numbers in string form. Your task is to 
convert this to an array of numbers.

Your function can only be a maximum of 30 characters long 
(not including whitespaces)! I have limited the char count 
because there is a very short and easy way to achieve this task.

Here is an example of what your function needs to return:

['1','2','3'] => [1,2,3]
Edge Cases:

1 - If your function comes up against a value that isn't a number
 its place in the array must be substituted with NaN.

2 - An empty array must return an empty array.

解法

这道题主要是有一个代码字数限制,只能在30个字符以内,使用了ES6的箭头函数

var convert = a => a.map(n => n*1)

题目:Thinkful - List and Loop Drills: Inverse Slicer

描述

You're familiar with list slicing in Python and know, 
for example, that:
>>> ages = [12, 14, 63, 72, 55, 24]
>>> ages[2:4]
[63, 72]
>>> ages[2:]
[63, 72, 55, 24]
>>> ages[:3]
[12, 14, 63]

write a function inverse_slice() that takes three arguments: 
a list items, an integer a and an integer b. The function should
 return a new list with the slice specified by items[a:b] excluded
For example:
>>>inverse_slice([12, 14, 63, 72, 55, 24], 2, 4)
[12, 14, 55, 24]

The input will always be a valid list, a and b will always be 
different integers equal to or greater than zero, but they may 
be zero or be larger than the length of the list.

解法

这道题也相对简单

function inverseSlice(items, a, b) {
  return items.filter((item, index) => !((index >= a) && (index < b)));
}

题目:Get all array elements except those with specified indexes

描述

Extend the array object with a function to return all elements
 of that array, except the ones with the indexes passed in the
  parameter.

For example:

var array = ['a', 'b', 'c', 'd', 'e'];
var array2 = array.except([1,3]);
// array2 should contain ['a', 'c', 'e'];
The function should accept both array as parameter but also a
 single integer, like this:

var array = ['a', 'b', 'c', 'd', 'e'];
var array2 = array.except(1);
// array2 should contain ['a', 'c', 'd', 'e'];

解法

我的解法

Array.prototype.except = function(keys)
{
  if (typeof keys === 'number') {
    return this.filter((item, index) => index !== keys);
  }
  if (Array.isArray(keys)) {
    return this.filter((item, index) => !keys.includes(index));
  }
  return -1;
}

大神的解法

Array.prototype.except = function(keys)
{
  if(!Array.isArray(keys)) keys = [keys];
  return this.filter((a,i) => !keys.includes(i));
}

题目:Sum of a Sequence [Hard-Core Version]

描述

The task is simple to explain: simply sum all the numbers from 
the first parameter being the beginning to the second parameter
 being the upper limit (possibly included), going in steps 
 expressed by the third parameter:

sequenceSum(2,2,2) === 2
sequenceSum(2,6,2) === 12 // 2 + 4 + 6
sequenceSum(1,5,1) === 15 // 1 + 2 + 3 + 4 + 5
sequenceSum(1,5,3) === 5 // 1 + 4
If it is an impossible sequence (with the beginning being larger
 the end and a positive step or the other way around), just 
 return 0. See the provided test cases for further examples :)

Note: differing from the other base kata, much larger ranges are
going to be tested, so you should hope to get your algo optimized
and to avoid brute-forcing your way through the solution.

解法

这道题有点意思,细心的人就会发现给出的例子里面可以看出一些等差数列的影子,那么我们
就可以按照等差数列的方法来解这道题了,等差数列求和公式:(首项 + 末项)* 项数 / 2

function sequenceSum(begin, end, step){
  //your code here 
  const n = Math.floor((end - begin) / step) + 1;
  if (n<0) return 0; 
  const An = begin + ((n-1) * step);
  return n * (begin + An) / 2;
}

注意:这里面我们需要求项数N和末项An,至于N怎么求的,那就要靠自己去发现规律


题目:Array.prototype.size()

描述

Implement Array.prototype.size() - without .length !

Implement Array.prototype.size(), which should simply return
the length of the array. But do it entirely without using
Array.prototype.length!
Where .length is a property, .size() is a method.

Rules

Because it is quite impossible to disable [].length, and because
filtering for "length" is an iffy proposition at best,
THIS KATA WORKS ON THE HONOUR SYSTEM.
You may cheat. But you may have trouble sleeping. Or $DEITY may
kill a puppy.

You need not support sparse arrays (but you may!). All testing
will be done with dense arrays.
Values will not be undefined. You need only support actual, real
arrays.

Your method needs to be read only. Arguments must be ignored. The
this object must not be modified.

解法

我的解法

Array.prototype.size = function () {
  var i = 0;
  while (this[i] !== undefined) i++;
  return i;
};

大神的解法

Array.prototype.size = function() {
  return this.reduce(r => r + 1, 0);
};

在这里看了下大神的解法,让我对reduce函数又有了新的理解


题目:Find the smallest integer in the array

描述

Find the smallest integer in the array.

Given an array of integers your solution should find the smallest
integer. For example:
Given [34, 15, 88, 2] your solution will return 2
Given [34, -345, -1, 100] your solution will return -345

You can assume, for the purpose of this kata, that the supplied
array will not be empty.

解法

我的解法

function findSmallestInt(args) {
  return args.sort((a, b) => {
    return a - b;
  })[0];
}

大神的解法

function findSmallestInt(args) {
  return Math.min(...args);
}

题目:Replace With Alphabet Position

描述

Welcome. In this kata you are required to, given a string,
replace every letter with its position in the alphabet. If 
anything in the text isn't a letter, ignore it and don't 
return it. a being 1, b being 2, etc. As an example:

alphabet_position("The sunset sets at twelve o' clock.")
Should return "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 
5 12 22 5 15 3 12 15 3 11" (As a string.)

解法

我的解法

function alphabetPosition(text) {
  let alphabet = {};
  let arr = [];
  const lowCaseText = text.toLowerCase();
  for (let i = 0; i < 26; i++) {
    alphabet[(String.fromCharCode((65+i))).toLowerCase()] = (i + 1);
  }
  for (let j = 0; j < text.length; j++) {
    if (alphabet[lowCaseText[j]]) {
      arr.push(alphabet[lowCaseText[j]]);
    }
  }
  return arr.join(' ');
}

大神的解法

function alphabetPosition(text) {
  var result = "";
  for (var i = 0; i < text.length; i++){
    var code = text.toUpperCase().charCodeAt(i)
    if (code > 64 && code < 91) result += (code - 64) + " ";
  }

  return result.slice(0, result.length-1);
}

题目:GetSum

描述

Given two integers, which can be positive and negative, find the
 sum of all the numbers between including them too and return it. 
 If both numbers are equal return a or b.

Note! a and b are not ordered!

Example: 
GetSum(1, 0) == 1   // 1 + 0 = 1
GetSum(1, 2) == 3   // 1 + 2 = 3
GetSum(0, 1) == 1   // 0 + 1 = 1
GetSum(1, 1) == 1   // 1 Since both are same
GetSum(-1, 0) == -1 // -1 + 0 = -1
GetSum(-1, 2) == 2  // -1 + 0 + 1 + 2 = 2

解法

依然是等差数列

function GetSum( a,b ) {
  //Good luck!
  return (a + b) * (Math.abs(b - a) + 1) / 2;
}

题目:Add Two Numbers

描述

You are given two linked lists representing two non-negative 
numbers. The digits are stored in reverse order
and each of their nodes contain a single digit. Add the two 
numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8

解法

javascript linked list structure

linked list 是一种链表结构,用于存储数字类似于:

// 807这个数字的存储结构
var num8 = {
  val: 8,
  next: null,
}

var num0 = {
  val: 0,
  next: num8,
}

var num7 = {
  val: 7,
  next: num0,
}
// 807这个数字就能够用这样的结构得到

就只是用一個新的linked list來儲存相加後的結果
要注意的就是list1跟list2長度可能不一樣
另外就是相加後可能比9還大,需要考慮進位的情況

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
    var list = new ListNode(0); //儲存輸出的結果,因為list的指針要不斷往後移,因此用一個假節點方便操作
    var result = list; // 使用一個ListNode來儲存相加的結果

    var sum,carry = 0; // carry用來處理進位

    //當 list1, list2 都沒有值,而且carry也為0的時候才結束迴圈
    while(l1 || l2 || carry > 0){
        sum = 0;

        // list1與list2長度可能不同,分開處理
        if(l1!== null){
            sum += l1.val;
            l1 = l1.next;
        }

        if(l2!==null){
            sum += l2.val;
            l2 = l2.next;
        }

        // 如果之前有進位,carry = 1;沒有的話carry = 0
        sum += carry;
        list.next = new ListNode(sum%10); //相加如果超過9,只能留下個位數放入結果list,十位數的地方進位
        carry = parseInt(sum/10);

        // list指標向後
        list = list.next;
    }
    // 因為第一個節點為假節點,跳過
    return result.next;
}
// The complete code for the List constructor is:
function List() {
 List.makeNode = function() { 
  return {data: null, next: null}; 
 }; 
 
 this.start = null; 
 this.end = null; 
 
 this.add = function(data) { 
  if (this.start === null) { 
   this.start = List.makeNode(); 
   this.end = this.start; 
  } else { t
   this.end.next = List.makeNode(); 
   this.end = this.end.next; 
  } ; 
  this.end.data = data; 
 }; 

 this.delete = function(data) { 
  var current = this.start; 
  var previous = this.start; 
  while (current !== null) { 
   if (data === current.data) { 
    if (current === this.start) { 
     this.start = current.next; 
     return; 
    } 
    if (current === this.end) 
                      this.end = previous;
    previous.next = current.next; return; 
    }
    previous = current; 
    current = current.next; 
   }
 }; 

 this.insertAsFirst = function(d) { 
  var temp = List.makeNode(); 
  temp.next = this.start; 
  this.start = temp; 
  temp.data = d; 
 }; 

 this.insertAfter = function(t, d) { 
  var current = this.start; 
  while (current !== null) { 
   if (current.data === t) { 
    var temp = List.makeNode();
    temp.data = d; 
    temp.next = current.next; 
    if (current === this.end) this.end = temp;
    current.next = temp; 
    return; 
   } 
   current = current.next; 
   }
  };

  this.item = function(i) { 
   var current = this.start; 
   while (current !== null) { 
    i--; 
    if (i === 0) return current; 
    current = current.next; 
   } 
   return null; 
  }; 

 this.each = function(f) {
  var current = this.start;
  while (current !== null) { 
   f(current); 
   current = current.next; 
  } 
 };
} 

题目:Reverse String

描述

Write a function that takes a string as input and returns the string reversed.
Example: Given s = "hello", return "olleh".

解法

我的解法

"hello" -> ['h','e','l','l','o'] -> 'o'+'l'+'l'+'e'+'h'

var reverseString = function(s) {
    var result = "";
    var ary = s.split("");

    for(var i = ary.length-1 ; i >= 0 ; i--){
        result = result + ary[i];
    }
    return result;
};

进阶解法

未交換前 ['h','e','l','l','o']
第1次交換 ['h','e','l','l','o'] o,h互換 ['o','e','l','l','h']

第2次交換 ['o','e','l','l','h'] e,l互換 ['o','l','l','e','h']

var reverseString = function(s) {
    var result = "";
    var ary = s.split("");
    for(var i = 0, max = (ary.length-1)/2 ; i < max   ; i++){
        var temp = ary[i];
        ary[i] = ary[ary.length - 1 - i];
        ary[ary.length - 1 - i] = temp;
    }
    return ary.join("");
};

题目:Valid Anagram

描述

Given two strings s and t, write a function to determine if t is 
an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

Note:
You may assume the string contains only lowercase alphabets.

Follow up:
What if the inputs contain unicode characters? How would you 
adapt your solution to such case?

解法

我的解法

要比較兩個字串裡面的字元是否相同,首先可以判斷長度是否相等,不相等就可以直接判定

為false 接下來將重新排序後的字串比較是否相等。

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isAnagram = function(s, t) {
    if(s.length !== t.length) return false;

    var s1 = s.split("").sort().join("");
    var t1 = t.split("").sort().join("");

    return s1 === t1;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python可以很方便地创建一个单项选择的系统,它可以用于学生在线测试或者教育培训场景中。 首先,需要开发一个用户界面,允许用户选择答案并提交他们的选择。 其次,在后台开发一个目和答案的数据库,同时开发一个算法来评分用户的选择,并显示正确的答案和用户的得分。 最后,为了让系统更加用户友好,还可以添加一些诸如进度条、计时器和声音效果等功能,以增强用户体验。另外,还需要充分考虑数据的安全性和程序的健壮性,以确保系统在实际应用中的稳定性和安全性。 在开发单项选择系统的过程中,可以使用Python语言中的Flask框架或者Django框架来快速创建一个Web应用程序,并使用MySQL或者SQLite等数据库管理系统来存储和管理数据。通过合理的设计和开发,可以让Python单项选择系统在实际应用中发挥它的优势,帮助学生或者使用者更高效、更快速地学习和提高知识。 ### 回答2: Python可以很方便地实现单项选择系统,具体步骤如下: 1. 定义目数据结构。可以使用字典或嵌套列表的形式保存目和答案选项等信息。 2. 编写函数打印目及选项。函数主要实现将目和选项输出给用户,可以使用循环语句来遍历每道的内容及选项。 3. 编写函数判断答案是否正确。当用户选择答案后,可以将其作为参数传给函数,然后在函数内部进行判断,返回布尔值表示是否正确。 4. 设计交互流程。可以使用while循环来实现交互流程,让用户输入答案并判断是否正确,然后输出结果并进入下一,直到答完所有目。 5. 加入随机出函数。可以使用random模块来实现随机出功能,这样每次运行程序用户都将会面对不同的目。 在实现单项选择系统的过程中,需要注意细节,例如输入格式、正确答案的判断标准等。此外,还可以加入计分、难度选择等功能,提高系统的可性。 ### 回答3: Python可以很容易地设计一个单项选择系统,可以通过使用Python的各种库和模块来实现。比如,可以使用Tkinter库来设计一个GUI界面,使用SQLite来保存问和答案信息。 在这个系统中,我们需要首先设计一个问和答案的数据库,其中包括目、选项和正确答案。然后我们需要编写一个主程序,以及一些附属函数,比如生成目、检查答案、计算得分等。 我们可以使用Tkinter库来设计一个界面,包括开始测试界面、答界面和结果界面。在开始测试界面,我们需要加载数据库中的问和选项,并提示用户单击开始测试按钮来开始测试。在答界面,我们需要将问和选项显示在GUI上,并返回选项的答案。用户可以单击选项,以选择自己的响应,并提交答案。系统会自动检查答案,并在结果界面上显示结果、分数以及答对的目。 在编写这个系统的过程中,我们还需要关注一些细节和问,比如如何显示目和选项、如何检查答案的正确性、如何处理多次选择、如何正确计算得分等等。同时,我们还需要考虑如何将这个系统部署到实际使用中,包括如何进行测试、评估和优化等方面。 总之,使用Python编写一个单项选择系统是很容易的,但需要我们有一定的编程基础和系统设计能力,以及对Python库和模块的熟悉程度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值