有人可以在有条件的情况下帮助我吗?

时间:2018-09-25 05:38:10

标签: ruby

我有此代码:

SELECT
a.date,
SUM(CASE WHEN b.quantity IS NULL THEN 0 ELSE b.quantity END) AS quantity
FROM date a
LEFT JOIN 
stock b on a.date BETWEEN b.Incoming_Date AND b.Outgoing_Date
GROUP BY a.date

我需要使用其他方法来测试我的回文法 条件。如果第一个回文返回def palindrome(string) string.downcase.split(/\W+/).join('') == string.downcase.split(/\W+/).join('').reverse end ,则需要打印true。如果另一个回文是yes is a palindrome,我需要打印false

使用此测试代码,

is not a palindrome. :(

我得到以下输出:

p palindrome("Dennis, Nell, Edna, Leon, Nedra, Anita, Rolf, Nora, Alice, Carol, Leo, Jane, Reed, Dena, Dale, Basil, Rae, Penny, Lana, Dave, Denny, Lena, Ida, Bernadette, Ben, Ray, Lila, Nina, Jo, Ira, Mara, Sara, Mario, Jan, Ina, Lily, Arne, Bette, Dan, Reba, Diane, Lynn, Ed, Eva, Dana, Lynne, Pearl, Isabel, Ada, Ned, Dee, Rena, Joel, Lora, Cecil, Aaron, Flora, Tina, Arden, Noel, and Ellen sinned.")
p palindrome("Depardieu, go razz a rogue I draped")
p palindrome("Desserts I stressed.")
p palindrome("Detartrated.")
p palindrome("Devo met a Mr., eh, DNA and her mate moved.")
p palindrome("Di as dad said.")
p palindrome("Did I draw Della too tall, Edward? I did?")
p palindrome("Dior droid.")
p palindrome("DNA-land.")
p palindrome("Do geese see god?")
p palindrome("Do good? I? No. Evil anon I deliver. I maim nine more hero-men in Saginaw, sanitary sword a-tuck, Carol, I. Lo! Rack, cut a drowsy rat in Aswan. I gas nine more hero-men in Miami. Reviled, I (Nona) live on. I do, O God.")
p palindrome("abracadabra!")
p palindrome("Mister, mister, on a see-saw with your sister.")
p palindrome("Almost every sentence is NOT a palindrome! How unfair!")

2 个答案:

答案 0 :(得分:1)

您可以编写一个小函数来检查其输入是对还是错,并打印所需的行。

def is_palindrome(string)
  p palindrome(string) ? "yes is a palindrome" : "is not a palindrome. :("
end

is_palindrome("Depardieu, go razz a rogue I draped")

也就是说,有很多非常非常好的Ruby测试库。我真的很喜欢rspec。这是一个例子。

RSpec.describe "palindrome" do
  it "ignores case" do
    expect( palindrome("Detartrated") ).to be true
  end

  it 'ignores non-alpha numerics' do
    expect( palindrome("DNA-land.") ).to be true
  end

  it 'ignores spaces' do
    expect( palindrome("Dior droid.") ).to be true
  end

  it 'finds non-palindromes' do
    expect( palindrome("abracadabra!") ).to be false
  end
end

gem install rspec,然后与rspec一起运行。

$ rspec ~/tmp/test.rb
....

Finished in 0.00442 seconds (files took 0.14683 seconds to load)
4 examples, 0 failures

对于一个小功能来说,这似乎有些过头,但是当您进行更复杂的创建时,熟悉一个好的测试库将非常重要。它有助于组织和描述您要测试的方案。如果测试失败,它将为您提供良好的信息。

  1) palindrome failed palindrome
     Failure/Error: expect( palindrome("slow speed: deep owls") ).to be true

       expected true
            got false
     # /Users/schwern/tmp/test.rb:23:in `block (2 levels) in <top (required)>'

答案 1 :(得分:0)

请注意,Process.join与此相同:

    class StoreAdmin(SummernoteModelAdmin):

        def save_model(self, request, obj, form, change):
            # adding the entry for the first time
            if not change:
                obj.created_by = request.user

            # updating already existing record
            else:
                obj.updated_by = request.user

            obj.save()

答案:

string.downcase.split(/\W+/).join('') == string.downcase.split(/\W+/).join('').reverse ? 'yes is a palindrome' : ' is not a palindrome. :('

这将返回:

if string.downcase.split(/\W+/).join('') == string.downcase.split(/\W+/).join('').reverse
'yes is a palindrome'
else
' is not a palindrome. :('
end