在Python中计算序列的总和

时间:2016-10-27 15:03:45

标签: python

我得到一个10位数字,需要用它做这样的事情:

1 * 1stdigit + 2 * 2nddigit + 3 * 3rddigit + ... + 9 * 9thdigit + 1 * 10thdigit = sum

例如,如果数字是9876543210,它将如下所示:

1 * 9 + 2 * 8 + 3 * 7 + 4 * 6 + 5 * 5 + 6 * 4 + 7 * 3 + 8 * 2 + 9 * 1 + 1 * 0

对于我的生活,我无法弄清楚如何做到这一点。所以任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

你可以在数字的字符串表示上使用GET example it "renders the #show view" do food = create(:food) get :show, params: { id: food.id } expect(response).to render_template :show end CREATE Example it "redirects to user page of user who uploaded food" do food_params = FactoryGirl.attributes_for(:food) post :create, :food => food_params expect(response).to redirect_to user_path(@user.id) end SHOW example it "assigns the requested food to @food" do food = create(:food) get :show, params: { id: food.id } expect(assigns(:food)).to eq(food) end DELETE it "deletes the food" do expect{ delete :destroy, id: @food}.to change(Food, :count).by(-1) end it "redirects to user page of user who deleted food" do delete :destroy, id: @food expect(response).to redirect_to user_path(@user.id) end PUT it "located the requested @food" do put :update, id: @food, food: FactoryGirl.attributes_for(:food) expect(assigns(:food)).to eq(@food) end it "changes @food's attributes" do put :update, id: @food, food: FactoryGirl.attributes_for(:food, title: "Yummers", kind: "Salad") @food.reload expect(@food.title).to eq("Yummers") expect(@food.kind).to eq("Salad") end 并乘以索引mod 10,从1开始:

enumerate