CocoaRocket

A custom development posts

Write a program that accepts a credit card number as a command-line argument. The program should print the card's type (or Unknown) as well a Valid/Invalid indication of whether or not the card passes the Luhn algorithm. Hint: use git, cover with specs, make some user friendly interface (can be a command line), make it modular.

Before a credit card is submitted to a financial institution, it generally makes sense to run some simple reality checks on the number. The numbers are a good length and it's common to make minor transcription errors when the card is not scanned directly. The first check people often do is to validate that the card matches a known pattern from one of the accepted card providers. Some of these patterns are:

```
+============+=============+===============+
| Card Type | Begins With | Number Length |
+============+=============+===============+
| AMEX | 34 or 37 | 15 |
+------------+-------------+---------------+
| Discover | 6011 | 16 |
+------------+-------------+---------------+
| MasterCard | 51-55 | 16 |
+------------+-------------+---------------+
| Visa | 4 | 13 or 16 |
+------------+-------------+---------------+
```

All of these card types also generate numbers such that they can be validated by the Luhn algorithm, so that's the second check systems usually try.

The steps are:

- Starting with the next to last digit and continuing with every other digit going back to the beginning of the card, double the digit
- Sum all doubled and untouched digits in the number
- If that total is a multiple of 10, the number is valid

For example, given the card number 4408 0412 3456 7893:

- 8 4 0 8 0 4 2 2 6 4 10 6 14 8 18 3
- 8+4+0+8+0+4+2+2+6+4+1+0+6+1+4+8+1+8+3 = 70
- 70 % 10 == 0 #=> Thus that card is valid.

Let's try one more, 4417 1234 5678 9112:

- 8 4 2 7 2 2 6 4 10 6 14 8 18 1 2 2
- 8+4+2+7+2+2+6+4+1+0+6+1+4+8+1+8+1+2+2 = 69
- 69 % 10 != 0 #=> That card is not valid.

**###########################################################################**

**SELUTION**

https://github.com/cocoarocket/CardCheck

#!/usr/bin/env ruby format_string = ARGV.first.delete(" ").split("").reverse #format_string = "4408 0412 3456 7893" class CardValidator attr_accessor :result_string, :format_string def initialize(args) @result_string = [] @format_string = args #.delete(" ").split("").reverse end def valid? to_translate % 10 == 0 ? true : false end private def to_translate @format_string.each_with_index do |element, index| #index.even? ? @result_string << (element.to_i * 2) : @result_string << element.to_i @result_string << index.even? ? (element.to_i * 2) : @result_string << element.to_i end @result_string = @result_string.reverse!.join.split("").map{|x| x.to_i }.inject(0, :+) end end if not format_string.empty? @CardTest = CardValidator.new(format_string) if @CardTest.valid? puts "Thus that card is valid" else puts "That card is not valid" end end

A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.

For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps.

Write a function:

def solution(n)

that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.

For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5.

Assume that:

N is an integer within the range [1..2,147,483,647].

Complexity:

expected worst-case time complexity is O(log(N));

expected worst-case space complexity is O(1).

**################################################################****SOLUTION**

#ruby2.2.3 n = [Random.new.rand(1..2000)] def solution(n) if n.class == Array an2 = n[0].to_s(2).scan(/(?<=1)[0]+(?=1)/).flatten else an2 = n.to_s(2).scan(/(?<=1)[0]+(?=1)/).flatten end if an2.empty? 0 else an2.max_by(&:length).length end end

A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e.

A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].

Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.

For example, consider the following array A consisting of N = 8 elements:

A[0] = -1

A[1] = 3

A[2] = -4

A[3] = 5

A[4] = 1

A[5] = -6

A[6] = 2

A[7] = 1

P = 1 is an equilibrium index of this array, because:

A[0] = −1 = A[2] + A[3] + A[4] + A[5] + A[6] + A[7]

P = 3 is an equilibrium index of this array, because:

A[0] + A[1] + A[2] = −2 = A[4] + A[5] + A[6] + A[7]

P = 7 is also an equilibrium index, because:

A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] = 0

and there are no elements with indices greater than 7.

P = 8 is not an equilibrium index, because it does not fulfill the condition 0 ≤ P < N.

Write a function:

def solution(a)

that, given a zero-indexed array A consisting of N integers, returns any of its equilibrium indices. The function should return −1 if no equilibrium index exists.

For example, given array A shown above, the function may return 1, 3 or 7, as explained above.

Assume that:

N is an integer within the range [0..100,000];

each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].

#################################################################

**SOLUTION**

Puzzle wrote not a programmer obviously, because the numbers in programming languages are not separated by commas if they are integer.

so I wrote a decision based what is a set of elements.

and in the beginning of the code there is test class for **Range****.**

#ruby2.2.3 def solution(a) eqi = [] i = a.index{|x| x.class == Range} a[i] = (a[i].first).downto(a[i].last).map {|g| g} if a[i].first > a[i].last a[i] = a[i].to_a if a[i].first < a[i].last a.flatten! a.each_with_index do |e,index| a1 = a.slice(0,index + 1) a2 = a.slice(index,a.size) r1 = a1.inject(0) { |sum,x| sum + x } r2 = a2.inject(0) { |sum,x| sum + x } if r1 == r2 eqi << true else eqi << false end end eqi.count(true) #count of the equilibrium index end

Deployment looks much easier than commonly thought.

We agree on the fact that you have already configured SSH access to your hosting server.

- go to the hosting via the console via ssh.
- create a git repository that is a folder:
**$ mkdir mygitfolder.git** - go to the folder mygitfolder.git:
**$ cd mygitfolder.git** - initialize the folder as a pure repository
**$ git --bare init** - in the local working directory you need to add in the git url of the repository that would then upload it to this url:
**$ git remote add origin my_login_name@my_hosting.net:mygitfolder.git** - next you need to upload the local working directory with the app in a folder on the server:
**$ git push origin master** - you can then use capostrano for deployment:
**$ cap production deploy**

That's about it. it usually looks directly prepare and uploading applications to the repository on the hosting.