• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Trouble with list slicing and inserting an item in a specific position on a list

Resolved: Trouble with list slicing and inserting an item in a specific position on a list

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m getting a bit more used to python now, and my professor has started teaching us about lists and string manipulation, list slicing and all of that. He proposed an exercise which I’m pretty sure can be solved by just using list slices and should not need super specific inbuilt python functions, but I have no clue as in how to start thinking about this problem. It goes as follows:
“Create a function, lets call it “generate_n_gaps”, that has as parameters a string ‘dna’ of any length that includes any combination of the characters in the string DNA = ‘ATCG’ (just like the dna sequencing we see in biology) and a gap that we will denote as GAP = ‘_’ , and an integer parameter ‘n’. The function must return a list with all variations of ‘dna’ containing up to n extra gaps without repetition.”
Here are a couple of examples:
The function should be defined as follows:
As requested, I have worked on the problem a bit and have written a code that manages to generate the anagrams needed. I managed to write another function that generates only one gap, and used that for the main one.
This manages to get me the anagrams I need, but the list my function returns is a bit messy, how would I go about cleaning it up? By that, I mean, is there any way to ‘combine’ the lists, removing the other lists inside the main list? I reckon if I manage to do that, removing the duplicates is not really an issue.
This is what my function returns for the first example:

Answer:

this will flatten your list of list. hope it helps.

If you have better answer, please add a comment about this, thank you!

combinations list python slice
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: VBA – Applying border around the areas with value/text

01/04/2023

Resolved: How can I implement a function depending on picked up items?

01/04/2023

Resolved: Azuer Service Bus SDK not receiving the specified messages

01/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.