Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Last active May 2, 2023 23:21
Show Gist options
  • Save thomasjslone/518292f018a55915c03422e5394ba517 to your computer and use it in GitHub Desktop.
Save thomasjslone/518292f018a55915c03422e5394ba517 to your computer and use it in GitHub Desktop.
i guided chat gpt into creating a working version of my method, it took a few tries and one tweak to get working.
def splice(b, e)
if !b.is_a?(String) || !e.is_a?(String) ; raise "Arguments require String type."
elsif b.empty? || e.empty? ; raise "Arguments cannot be empty."
end
s_copy = self.dup
if s_copy.length <= (b.length + e.length); raise "Base string is too small."; end
pos = 0 ; stack = false ; list = []
if b.length > e.length ; buffer_length = b.length
else ; buffer_length = e.length
end
buffer = Array.new(buffer_length, "")
empty_buffer = Array.new(buffer_length, "")
empty_buffer2 = Array.new(buffer_length, "")
tag1 = empty_buffer
b.split('').each do |ch|
empty_buffer << ch
empty_buffer.delete_at(0)
end
tag2 = empty_buffer2
e.split('').each do |ch|
empty_buffer2 << ch
empty_buffer2.delete_at(0)
end
loop do
if s_copy[pos].nil? ; break; end
buffer << s_copy[pos]
buffer.delete_at(0)
if stack ; list << s_copy[pos] ; end
str = buffer.join('')
tag = tag1.join('').reverse
m = true; i = 0
tag.split('').each do |ch|
if ch != str.reverse[i] && ch != "" ; m = false; break ; end
i += 1
end
if m ; stack = true; end
tag = tag2.join('').reverse
m = true ; i = 0
tag.split('').each do |ch|
if ch != str.reverse[i] && ch != ""; m = false; break; end
i += 1
end
if m ; stack = false; end
pos += 1
end
if list.empty? ; return nil
else ; return list.join('')[0..("-#{e.length + 1}").to_i]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment