Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please let me know what the error for this code is? My output is the same as my input. Thank you! Problem :

Can you please let me know what the error for this code is? My output is the same as my input. Thank you!

Problem:

Swap Elements / Programming challenge description:

You are given a list of numbers which is supplemented with positions that have to be swapped.

Input:

Your program should read lines from standard input. Each line contains a list of space-delimited numbers, followed by a colon, followed by the indexes to be swapped. The first position in the list is at index 0. If there is more than one pair of indexes to be swapped, process each pair in the order they appear in the input, left to right.

Output:

Print the new list.

Test 1

Test InputDownload Test 1 Input

1 2 3 4 5 6 7 8 9 : 0-8

Expected OutputDownload Test 1 Output

9 2 3 4 5 6 7 8 1

Test 2

Test InputDownload Test 2 Input

1 2 3 4 5 6 7 8 9 10 : 0-1, 1-3

Expected OutputDownload Test 2 Output

2 4 3 1 5 6 7 8 9 10

My Code:

import sys # import numpy as np # import pandas as pd # from sklearn import ...

for line in sys.stdin: print(line, end="") l=input() nums=l.split(" : ") numbers_list=nums[0] paris=nums[1].split(", ") numbers_list=list(map(int,numbers_list.split(" "))) for pair in pairs: ind1=int(pair[0]) ind2=int(pait[2]) numbers_list[ind1],numbers_list[ind2]=numbers_list[ind2],numbers_list[ind1] print(*numbers_list)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions