<commit_before># Class Warfare, Validate a Credit Card Number


# I worked on this challenge [by myself, with: ].
# I spent [#] hours on this challenge.

# Pseudocode

# Input: A 16-digt integer
# Output: Boolean
# Steps:

# create class CreditCard

# create initialize method that takes 1 argument (an integer)

# IF credit card number is not equal to 16 digits, you should receive an argument error.

# END

# Split up 16-digit integer into individual digits.

# Starting at 1st integer, double every other integer.

# Split this new list of numbers into individual digits to account for double digits in list.

# Add up all the digits.

# Divide results by 10.

# If no remainder, output true.

# If there is a remainder, output false.

# Initial Solution

# Don't forget to check on initialization for a card length
# of exactly 16 digits

class CreditCard
  def initialize(integer)
    @integer = integer
    if integer.to_s.length != 16
      raise ArgumentError.new "This isn't a 16 digit number."
    end
  end

  def split_add
    nums = @integer
    nums = nums.to_s.split('')
    @new_array = []
    (0..15).each do |x|
      if x.even?
        @new_array << (nums[x].to_i * 2)
      else
        @new_array << nums[x].to_i
      end
    end
  end

  def total
    split_add
    add_array = []
    @new_array = @new_array.join
    @new_array = @new_array.split('')
    @new_array.map do |x|
      add_array << x.to_i
    end
    @sum = add_array.inject do |sum, x|
      sum = sum + x
    end
  end

    def check_card
      total
      if @sum % 10 == 0
        true
      else
        false
      end
   end
end



# Refactored Solution








# Reflection

=begin
class CreditCard
  def initialize(integer)
    @integer = integer

    if integer.to_s.size != 16
      raise ArgumentError.new "Please enter a 16 digit number"
    end
  end

  def check_card
    require 'scanf'
    new_num = @integer.to_s.scanf("%1d" * @integer.to_s.size)
#     p new_num
    new_num = new_num.reverse
    new_num.each_with_index do |x, i| i.odd?
      if true
        x.length * 2
      else
        x.next
      end
    end
    p new_num
  end
end

test = CreditCard.new(1234567891234567)

p test.check_card

=end<commit_msg>CreditCard<commit_after># Class Warfare, Validate a Credit Card Number


# I worked on this challenge [by myself, with: ].
# I spent [#] hours on this challenge.

# Pseudocode

# Input: A 16-digt integer
# Output: Boolean
# Steps:

# create class CreditCard

# create initialize method that takes 1 argument (an integer)

# IF credit card number is not equal to 16 digits, you should receive an argument error.

# END

# Split up 16-digit integer into individual digits.

# Starting at 1st integer, double every other integer.

# Split this new list of numbers into individual digits to account for double digits in list.

# Add up all the digits.

# Divide results by 10.

# If no remainder, output true.

# If there is a remainder, output false.

# Initial Solution

# Don't forget to check on initialization for a card length
# of exactly 16 digits

# class CreditCard
#   def initialize(integer)
#     @integer = integer
#     if integer.to_s.length != 16
#       raise ArgumentError.new "This isn't a 16 digit number."
#     end
#   end

#   def split_add
#     nums = @integer
#     nums = nums.to_s.split('')
#     @new_array = []
#     (0..15).each do |x|
#       if x.even?
#         @new_array << (nums[x].to_i * 2)
#       else
#         @new_array << nums[x].to_i
#       end
#     end
#   end

#   def total
#     split_add
#     add_array = []
#     @new_array = @new_array.join
#     @new_array = @new_array.split('')
#     @new_array.map do |x|
#       add_array << x.to_i
#     end
#     @sum = add_array.inject do |sum, x|
#       sum = sum + x
#     end
#   end

#     def check_card
#       total
#       if @sum % 10 == 0
#         true
#       else
#         false
#       end
#    end
# end



# Refactored Solution

class CreditCard
  def initialize(integer)
    @integer = integer
      raise ArgumentError.new "This isn't a 16 digit number." if integer.to_s.length != 16
    end
  end

  def split_add
    nums = @integer
    nums = nums.to_s.split('')
    @new_array = []
    (0..15).each do |x|
      if x.even?
        @new_array << (nums[x].to_i * 2)
      else
        @new_array << nums[x].to_i
      end
    end
  end

  def total
    split_add
    add_array = []
    @new_array = @new_array.join.split('')
    @new_array.map do |x|
      add_array << x.to_i
    end
    @sum = add_array.inject do |sum, x|
      sum = sum + x
    end
  end

    def check_card
      total
      if @sum % 10 == 0
        true
      else
        false
      end
   end
end






# Reflection

# What was the most difficult part of this challenge for you and your pair?

# We struggled with going through each index in the array and modifying the element by multiplying it by two. My pair used his own solution by interating with the method scanf. He understood it but I struggled getting past that method as I wasn't familiar or found it readable. So I went and approached it by using a range.

# What new methods did you find to help you when you refactored?

# I conversed with Katy in office hours and she told me there isn't much to refactor other than to not repeat the assigning of the new_array with two methods such as the .join and the .split

# What concepts or learnings were you able to solidify in this challenge?

# accessing other methods within a method doesn't have to have an instance variable if you want to just run the method.

{'fraction_non_alphanumeric': 0.08319914117015566, 'fraction_numerical': 0.014134907854714617, 'mean_word_length': 2.4231475811390077, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7372412', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1771, 'n_words': 695}